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

@ -10,7 +10,8 @@ int main()
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

@ -116,7 +116,8 @@
return change; return change;
} }
- (NSString *)keypath { - (NSString *) keypath
{
return _keypath; return _keypath;
} }
@ -146,7 +147,11 @@
- (void) setInfo: (NSDictionary *)newInfo - (void) setInfo: (NSDictionary *)newInfo
{ {
ASSIGN(_info, [newInfo copy]); if (newInfo != _info)
{
[_info release];
_info = [newInfo copy];
}
} }
- (void *) context - (void *) context
@ -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

@ -11,14 +11,16 @@ int main()
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]
initForReadingWithData: nil]);,
@"NSInvalidArgumentException", @"NSInvalidArgumentException",
"Creating an NSUnarchiver with nil data throws an exception"); "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,11 +39,8 @@ 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"];
PASS_RUNS(val1 = [NSString stringWithCString:"Archiver.dat"];
val2 = [NSString stringWithCString:"A Goodbye"]; val2 = [NSString stringWithCString:"A Goodbye"];
val3 = [NSString stringWithCString:"Testing all strings"]; val3 = [NSString stringWithCString:"Testing all strings"];
val4 = [NSNumber numberWithUnsignedInt: 100]; val4 = [NSNumber numberWithUnsignedInt: 100];
@ -50,7 +48,7 @@ int main()
arrayByAddingObject: val2] arrayByAddingObject: val2]
arrayByAddingObject: val4]; arrayByAddingObject: val4];
vals2 = [vals1 arrayByAddingObject: val2];, vals2 = [vals1 arrayByAddingObject: val2];,
"We can build basic strings and arrays for tests"); "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");
@ -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);