Fix some more leaks

This commit is contained in:
rfm 2024-11-15 22:31:41 +00:00
parent 9573539795
commit 87c50830e2
5 changed files with 78 additions and 66 deletions

View file

@ -4,13 +4,14 @@
int main() int main()
{ {
NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSDictionary *dict; NSDictionary *dict;
NSArray *cookies; NSArray *cookies;
NSURL *url; NSURL *url;
NSHTTPCookie *cookie; NSHTTPCookie *cookie;
TEST_FOR_CLASS(@"NSHTTPCookie", [NSHTTPCookie alloc], cookie = AUTORELEASE([NSHTTPCookie new]);
TEST_FOR_CLASS(@"NSHTTPCookie", cookie,
"NSHTTPCookie +alloc returns an NSHTTPCookie"); "NSHTTPCookie +alloc returns an NSHTTPCookie");
dict = [NSDictionary dictionaryWithObjectsAndKeys: @"myname", @"Name", dict = [NSDictionary dictionaryWithObjectsAndKeys: @"myname", @"Name",

View file

@ -103,10 +103,10 @@
@end @end
@implementation TestKVOChange @implementation TestKVOChange
+ (id)changeWithKeypath:(NSString *)keypath + (id) changeWithKeypath: (NSString *)keypath
object:(id)object object: (id)object
info:(NSDictionary *)info info: (NSDictionary *)info
context:(void *)context context: (void *)context
{ {
TestKVOChange *change = [[[self alloc] init] autorelease]; TestKVOChange *change = [[[self alloc] init] autorelease];
[change setKeypath: keypath]; [change setKeypath: keypath];
@ -116,55 +116,60 @@
return change; return change;
} }
- (NSString *)keypath { - (NSString *) keypath
return _keypath; {
return _keypath;
} }
- (void)setKeypath:(NSString *)newKeypath - (void) setKeypath: (NSString *)newKeypath
{ {
if (_keypath != newKeypath) if (_keypath != newKeypath)
{ {
[_keypath release]; [_keypath release];
_keypath = [newKeypath copy]; _keypath = [newKeypath copy];
} }
} }
- (id)object - (id) object
{ {
return _object; return _object;
} }
- (void)setObject:(id)newObject - (void) setObject: (id)newObject
{ {
ASSIGN(_object, newObject); ASSIGN(_object, newObject);
} }
- (NSDictionary *)info - (NSDictionary *)info
{ {
return _info; return _info;
} }
- (void)setInfo:(NSDictionary *)newInfo - (void) setInfo: (NSDictionary *)newInfo
{ {
ASSIGN(_info, [newInfo copy]); if (newInfo != _info)
{
[_info release];
_info = [newInfo copy];
}
} }
- (void *)context - (void *) context
{ {
return _context; return _context;
} }
- (void)setContext:(void *)newContext - (void) setContext:(void *)newContext
{ {
_context = newContext; _context = newContext;
} }
- (void)dealloc - (void) dealloc
{ {
[_object release]; [_object release];
[_keypath release]; [_keypath release];
[_info release]; [_info release];
[super dealloc]; [super dealloc];
} }
@end @end
@ -216,7 +221,7 @@
[_lock lock]; [_lock lock];
NSSet *paths = [[_changedKeypaths objectForKey:keypath] copy]; NSSet *paths = [[_changedKeypaths objectForKey:keypath] copy];
[_lock unlock]; [_lock unlock];
return paths; return AUTORELEASE(paths);
} }
- (void)clear - (void)clear
{ {

View file

@ -3,10 +3,15 @@
int main() int main()
{ {
NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSAutoreleasePool *arp = [NSAutoreleasePool new];
test_alloc_only(@"NSKeyedArchiver"); test_alloc_only(@"NSKeyedArchiver");
test_NSObject(@"NSKeyedArchiver",[NSArray arrayWithObject:[[NSKeyedArchiver alloc] initForWritingWithMutableData: [NSMutableData data]]]); test_NSObject(@"NSKeyedArchiver", [NSArray arrayWithObject:
AUTORELEASE([[NSKeyedArchiver alloc] initForWritingWithMutableData:
[NSMutableData data]])]);
test_alloc_only(@"NSKeyedUnarchiver"); test_alloc_only(@"NSKeyedUnarchiver");
test_NSObject(@"NSKeyedUnarchiver",[NSArray arrayWithObject:[[NSKeyedUnarchiver alloc] initForReadingWithData: [NSKeyedArchiver archivedDataWithRootObject: [NSData data]]]]); test_NSObject(@"NSKeyedUnarchiver", [NSArray arrayWithObject:
AUTORELEASE([[NSKeyedUnarchiver alloc] initForReadingWithData:
[NSKeyedArchiver archivedDataWithRootObject: [NSData data]]])]);
[arp release]; arp = nil; [arp release]; arp = nil;
return 0; return 0;

View file

@ -7,18 +7,20 @@
int main() int main()
{ {
NSAutoreleasePool *arp = [NSAutoreleasePool new]; NSAutoreleasePool *arp = [NSAutoreleasePool new];
id obj; id obj;
NSMutableData *data1; NSMutableData *data1;
obj = [NSKeyedArchiver alloc];
data1 = [NSMutableData dataWithLength: 0]; data1 = [NSMutableData dataWithLength: 0];
obj = [obj initForWritingWithMutableData: data1]; obj = AUTORELEASE([[NSKeyedArchiver alloc]
PASS((obj != nil && [obj isKindOfClass:[NSKeyedArchiver class]]), "-initForWritingWithMutableData seems ok"); initForWritingWithMutableData: data1]);
PASS((obj != nil && [obj isKindOfClass: [NSKeyedArchiver class]]),
"-initForWritingWithMutableData seems ok")
PASS_EXCEPTION([[NSUnarchiver alloc] initForReadingWithData:nil];, PASS_EXCEPTION(AUTORELEASE([[NSUnarchiver alloc]
@"NSInvalidArgumentException", initForReadingWithData: nil]);,
"Creating an NSUnarchiver with nil data throws an exception"); @"NSInvalidArgumentException",
"Creating an NSUnarchiver with nil data throws an exception")
[arp release]; arp = nil; [arp release]; arp = nil;
return 0; return 0;

View file

@ -27,8 +27,9 @@ int main()
u = [NSURL URLWithString: @"http://www.w3.org/"]; u = [NSURL URLWithString: @"http://www.w3.org/"];
ms = [NSMutableSet set]; ms = [NSMutableSet set];
[ms addObject: u]; [ms addObject: u];
data2 = [NSMutableData new]; data2 = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data2]; archiver = AUTORELEASE([[NSKeyedArchiver alloc]
initForWritingWithMutableData: data2]);
[archiver setOutputFormat: NSPropertyListXMLFormat_v1_0]; [archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];
[archiver encodeObject: ms forKey: @"root"]; [archiver encodeObject: ms forKey: @"root"];
[archiver finishEncoding]; [archiver finishEncoding];
@ -38,28 +39,25 @@ int main()
PASS([[[ms anyObject] absoluteString] isEqual: @"http://www.w3.org/"], PASS([[[ms anyObject] absoluteString] isEqual: @"http://www.w3.org/"],
"Can archive and restore a URL"); "Can archive and restore a URL");
[archiver release]; PASS_RUNS(
[data2 release]; val1 = [NSString stringWithCString:"Archiver.dat"];
val2 = [NSString stringWithCString:"A Goodbye"];
val3 = [NSString stringWithCString:"Testing all strings"];
PASS_RUNS(val1 = [NSString stringWithCString:"Archiver.dat"]; val4 = [NSNumber numberWithUnsignedInt: 100];
val2 = [NSString stringWithCString:"A Goodbye"]; vals1 = [[[NSArray arrayWithObject: val1]
val3 = [NSString stringWithCString:"Testing all strings"]; arrayByAddingObject: val2]
val4 = [NSNumber numberWithUnsignedInt: 100]; arrayByAddingObject: val4];
vals1 = [[[NSArray arrayWithObject:val1] vals2 = [vals1 arrayByAddingObject: val2];,
arrayByAddingObject:val2] "We can build basic strings and arrays for tests")
arrayByAddingObject: val4];
vals2 = [vals1 arrayByAddingObject: val2];,
"We can build basic strings and arrays for tests");
PASS([NSKeyedArchiver archiveRootObject:vals2 toFile:val1], PASS([NSKeyedArchiver archiveRootObject: vals2 toFile: val1],
"archiveRootObject:toFile: seems ok"); "archiveRootObject:toFile: seems ok");
data1 = [NSKeyedArchiver archivedDataWithRootObject:vals2]; data1 = [NSKeyedArchiver archivedDataWithRootObject: vals2];
PASS((data1 != nil && [data1 length] != 0), PASS((data1 != nil && [data1 length] != 0),
"archivedDataWithRootObject: seems ok"); "archivedDataWithRootObject: seems ok");
a = [NSKeyedUnarchiver unarchiveObjectWithData:data1]; a = [NSKeyedUnarchiver unarchiveObjectWithData: data1];
NSLog(@"From data: original array %@, decoded array %@",vals2, a); NSLog(@"From data: original array %@, decoded array %@",vals2, a);
PASS((a != nil && [a isKindOfClass:[NSArray class]] && [a isEqual:vals2]), PASS((a != nil && [a isKindOfClass:[NSArray class]] && [a isEqual:vals2]),
"unarchiveObjectWithData: seems ok"); "unarchiveObjectWithData: seems ok");
@ -70,17 +68,18 @@ int main()
"unarchiveObjectWithFile: seems ok"); "unarchiveObjectWithFile: seems ok");
// encode // encode
data2 = [[NSMutableData alloc] initWithCapacity: 10240]; data2 = [NSMutableData dataWithCapacity: 10240];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data2]; archiver = AUTORELEASE([[NSKeyedArchiver alloc]
initForWritingWithMutableData: data2]);
[archiver encodeObject: val3 forKey: @"string"]; [archiver encodeObject: val3 forKey: @"string"];
[archiver finishEncoding]; [archiver finishEncoding];
// decode... // decode...
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data2]; unarchiver = AUTORELEASE([[NSKeyedUnarchiver alloc]
s = [[unarchiver decodeObjectForKey: @"string"] retain]; initForReadingWithData: data2]);
s = [unarchiver decodeObjectForKey: @"string"];
PASS((s != nil && [s isKindOfClass:[NSString class]] && [s isEqual: val3]), PASS((s != nil && [s isKindOfClass:[NSString class]] && [s isEqual: val3]),
"encodeObject:forKey: seems okay"); "encodeObject:forKey: seems okay");
[data2 release];
NSLog(@"Original string: %@, unarchived string: %@",val3, s); NSLog(@"Original string: %@, unarchived string: %@",val3, s);