From 01b40beefb30fffbf1fd7d44a7b60eb3cbd23720 Mon Sep 17 00:00:00 2001 From: CaS Date: Sun, 11 Apr 2004 05:22:33 +0000 Subject: [PATCH] Retain/release fixes. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@19074 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 10 ++++++++++ Headers/Foundation/NSArchiver.h | 3 ++- Source/NSNumber.m | 29 ++++++++++++++--------------- Source/NSUnarchiver.m | 21 ++++++++++++++++++++- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 831338ebf..1e6f68a5c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2004-04-11 Richard Frith-Macdonald + + * Headers/Foundation/NSArchiver.h: New ivar in unarchiver. + * Source/NSUnarchiver.m: Simpler scheme to ensure that objects + persist until the end of unarchiving ... just store them in an + array. + * Source/NSNumber.m: Don't deallocate self in initialisers when + replacing with cached object ... use release instead in case + something else has retained us. + 2004-04-09 Gregory John Casamento * Source/NSUnarchiver.m: Temporary rollback of previous fix. diff --git a/Headers/Foundation/NSArchiver.h b/Headers/Foundation/NSArchiver.h index 53d85460b..2dbd378cf 100644 --- a/Headers/Foundation/NSArchiver.h +++ b/Headers/Foundation/NSArchiver.h @@ -31,7 +31,7 @@ #include -@class NSMutableDictionary, NSMutableData, NSData, NSString; +@class NSMutableArray, NSMutableDictionary, NSMutableData, NSData, NSString; @interface NSArchiver : NSCoder { @@ -163,6 +163,7 @@ unsigned version; /* Version of archiver used. */ NSZone *zone; /* Zone for allocating objs. */ NSMutableDictionary *objDict; /* Class information store. */ + NSMutableArray *objSave; } /* Initializing an unarchiver */ diff --git a/Source/NSNumber.m b/Source/NSNumber.m index 2fe15ee1b..287b8ff99 100644 --- a/Source/NSNumber.m +++ b/Source/NSNumber.m @@ -635,7 +635,7 @@ static Class doubleNumberClass; - (id) initWithBool: (BOOL)value { - NSDeallocateObject(self); + RELEASE(self); if (value == NO) { self = boolN; @@ -649,7 +649,7 @@ static Class doubleNumberClass; - (id) initWithChar: (signed char)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL && value >= -GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -662,7 +662,7 @@ static Class doubleNumberClass; - (id) initWithDouble: (double)value { - NSDeallocateObject(self); + RELEASE(self); self = (NSNumber*)NSAllocateObject(doubleNumberClass, 0, NSDefaultMallocZone()); self = [self initWithBytes: &value objCType: NULL]; @@ -671,7 +671,7 @@ static Class doubleNumberClass; - (id) initWithFloat: (float)value { - NSDeallocateObject(self); + RELEASE(self); self = (NSNumber*)NSAllocateObject(floatNumberClass, 0, NSDefaultMallocZone()); self = [self initWithBytes: &value objCType: NULL]; @@ -680,7 +680,7 @@ static Class doubleNumberClass; - (id) initWithInt: (signed int)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL && value >= -GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -693,7 +693,7 @@ static Class doubleNumberClass; - (id) initWithLong: (signed long)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL && value >= -GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -706,7 +706,7 @@ static Class doubleNumberClass; - (id) initWithLongLong: (signed long long)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL && value >= -GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -719,7 +719,7 @@ static Class doubleNumberClass; - (id) initWithShort: (signed short)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL && value >= -GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -732,7 +732,7 @@ static Class doubleNumberClass; - (id) initWithUnsignedChar: (unsigned char)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -745,7 +745,7 @@ static Class doubleNumberClass; - (id) initWithUnsignedInt: (unsigned int)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -758,7 +758,7 @@ static Class doubleNumberClass; - (id) initWithUnsignedLong: (unsigned long)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -771,7 +771,7 @@ static Class doubleNumberClass; - (id) initWithUnsignedLongLong: (unsigned long long)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -784,7 +784,7 @@ static Class doubleNumberClass; - (id) initWithUnsignedShort: (unsigned short)value { - NSDeallocateObject(self); + RELEASE(self); if (value <= GS_SMALL) { return RETAIN(smallIntegers[value + GS_SMALL]); @@ -2524,8 +2524,7 @@ static Class doubleNumberClass; case 'f': self = [self initWithFloat: data.f]; break; case 'd': self = [self initWithDouble: data.d]; break; default: - [self dealloc]; - self = nil; + DESTROY(self); NSLog(@"Attempt to decode number with unknown ObjC type"); } return self; diff --git a/Source/NSUnarchiver.m b/Source/NSUnarchiver.m index b1f7feb0a..6197c7559 100644 --- a/Source/NSUnarchiver.m +++ b/Source/NSUnarchiver.m @@ -49,6 +49,7 @@ #include "Foundation/NSData.h" #include "Foundation/NSUtilities.h" #include "Foundation/NSString.h" +#include "Foundation/NSArray.h" static const char* typeToName1(char type) @@ -391,6 +392,7 @@ static Class NSDataMallocClass; - (void) dealloc { RELEASE(data); + RELEASE(objSave); RELEASE(objDict); if (clsMap) { @@ -435,7 +437,13 @@ static Class NSDataMallocClass; * encoded with. */ objDict = [[NSMutableDictionary allocWithZone: zone] - initWithCapacity: 200]; + initWithCapacity: 200]; + /* + * objSave is an array used purely to ensure that objects + * we decode persist until the end of the decoding. + */ + objSave = [[NSMutableArray allocWithZone: zone] + initWithCapacity: 200]; NS_DURING { @@ -609,6 +617,16 @@ static Class NSDataMallocClass; obj = rep; GSIArraySetItemAtIndex(objMap, (GSIArrayItem)obj, xref); } + /* + * The objMap does not retain objects, so in order to + * be sure that a decoded object is not deallocated by + * anything before it is needed (because it is decoded + * later as a cross reference) we store it in objSave. + */ + if (obj != nil) + { + [objSave addObject: obj]; + } } } *(id*)address = obj; @@ -1269,6 +1287,7 @@ static Class NSDataMallocClass; } [objDict removeAllObjects]; + [objSave removeAllObjects]; } - (void) deserializeHeaderAt: (unsigned*)pos