Skip to content

Commit

Permalink
Fix some more leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Nov 16, 2024
1 parent 9573539 commit 87c5083
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 66 deletions.
13 changes: 7 additions & 6 deletions Tests/base/NSHTTPCookie/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSDictionary *dict;
NSArray *cookies;
NSURL *url;
NSHTTPCookie *cookie;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
NSDictionary *dict;
NSArray *cookies;
NSURL *url;
NSHTTPCookie *cookie;

TEST_FOR_CLASS(@"NSHTTPCookie", [NSHTTPCookie alloc],
cookie = AUTORELEASE([NSHTTPCookie new]);
TEST_FOR_CLASS(@"NSHTTPCookie", cookie,
"NSHTTPCookie +alloc returns an NSHTTPCookie");

dict = [NSDictionary dictionaryWithObjectsAndKeys: @"myname", @"Name",
Expand Down
57 changes: 31 additions & 26 deletions Tests/base/NSKVOSupport/general.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ - (void)setContext:(void *)newContext;
@end

@implementation TestKVOChange
+ (id)changeWithKeypath:(NSString *)keypath
object:(id)object
info:(NSDictionary *)info
context:(void *)context
+ (id) changeWithKeypath: (NSString *)keypath
object: (id)object
info: (NSDictionary *)info
context: (void *)context
{
TestKVOChange *change = [[[self alloc] init] autorelease];
[change setKeypath: keypath];
Expand All @@ -116,55 +116,60 @@ + (id)changeWithKeypath:(NSString *)keypath
return change;
}

- (NSString *)keypath {
return _keypath;
- (NSString *) keypath
{
return _keypath;
}

- (void)setKeypath:(NSString *)newKeypath
- (void) setKeypath: (NSString *)newKeypath
{
if (_keypath != newKeypath)
if (_keypath != newKeypath)
{
[_keypath release];
_keypath = [newKeypath copy];
[_keypath release];
_keypath = [newKeypath copy];
}
}

- (id)object
- (id) object
{
return _object;
return _object;
}

- (void)setObject:(id)newObject
- (void) setObject: (id)newObject
{
ASSIGN(_object, newObject);
}

- (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];
[_keypath release];
[_info release];
[super dealloc];
[_object release];
[_keypath release];
[_info release];
[super dealloc];
}

@end
Expand Down Expand Up @@ -216,7 +221,7 @@ - (NSSet *)changesForKeypath:(NSString *)keypath
[_lock lock];
NSSet *paths = [[_changedKeypaths objectForKey:keypath] copy];
[_lock unlock];
return paths;
return AUTORELEASE(paths);
}
- (void)clear
{
Expand Down
9 changes: 7 additions & 2 deletions Tests/base/NSKeyedArchiver/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];

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_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;
return 0;
Expand Down
20 changes: 11 additions & 9 deletions Tests/base/NSKeyedArchiver/create.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

int main()
{
NSAutoreleasePool *arp = [NSAutoreleasePool new];
id obj;
NSMutableData *data1;
NSAutoreleasePool *arp = [NSAutoreleasePool new];
id obj;
NSMutableData *data1;

obj = [NSKeyedArchiver alloc];
data1 = [NSMutableData dataWithLength: 0];
obj = [obj initForWritingWithMutableData: data1];
PASS((obj != nil && [obj isKindOfClass:[NSKeyedArchiver class]]), "-initForWritingWithMutableData seems ok");
obj = AUTORELEASE([[NSKeyedArchiver alloc]
initForWritingWithMutableData: data1]);
PASS((obj != nil && [obj isKindOfClass: [NSKeyedArchiver class]]),
"-initForWritingWithMutableData seems ok")

PASS_EXCEPTION([[NSUnarchiver alloc] initForReadingWithData:nil];,
@"NSInvalidArgumentException",
"Creating an NSUnarchiver with nil data throws an exception");
PASS_EXCEPTION(AUTORELEASE([[NSUnarchiver alloc]
initForReadingWithData: nil]);,
@"NSInvalidArgumentException",
"Creating an NSUnarchiver with nil data throws an exception")

[arp release]; arp = nil;
return 0;
Expand Down
45 changes: 22 additions & 23 deletions Tests/base/NSKeyedArchiver/general.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ int main()
u = [NSURL URLWithString: @"http://www.w3.org/"];
ms = [NSMutableSet set];
[ms addObject: u];
data2 = [NSMutableData new];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data2];
data2 = [NSMutableData data];
archiver = AUTORELEASE([[NSKeyedArchiver alloc]
initForWritingWithMutableData: data2]);
[archiver setOutputFormat: NSPropertyListXMLFormat_v1_0];
[archiver encodeObject: ms forKey: @"root"];
[archiver finishEncoding];
Expand All @@ -38,28 +39,25 @@ int main()
PASS([[[ms anyObject] absoluteString] isEqual: @"http://www.w3.org/"],
"Can archive and restore a URL");

[archiver release];
[data2 release];


PASS_RUNS(val1 = [NSString stringWithCString:"Archiver.dat"];
val2 = [NSString stringWithCString:"A Goodbye"];
val3 = [NSString stringWithCString:"Testing all strings"];
val4 = [NSNumber numberWithUnsignedInt: 100];
vals1 = [[[NSArray arrayWithObject:val1]
arrayByAddingObject:val2]
arrayByAddingObject: val4];
vals2 = [vals1 arrayByAddingObject: val2];,
"We can build basic strings and arrays for tests");
PASS_RUNS(
val1 = [NSString stringWithCString:"Archiver.dat"];
val2 = [NSString stringWithCString:"A Goodbye"];
val3 = [NSString stringWithCString:"Testing all strings"];
val4 = [NSNumber numberWithUnsignedInt: 100];
vals1 = [[[NSArray arrayWithObject: val1]
arrayByAddingObject: val2]
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");

data1 = [NSKeyedArchiver archivedDataWithRootObject:vals2];
data1 = [NSKeyedArchiver archivedDataWithRootObject: vals2];
PASS((data1 != nil && [data1 length] != 0),
"archivedDataWithRootObject: seems ok");

a = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
a = [NSKeyedUnarchiver unarchiveObjectWithData: data1];
NSLog(@"From data: original array %@, decoded array %@",vals2, a);
PASS((a != nil && [a isKindOfClass:[NSArray class]] && [a isEqual:vals2]),
"unarchiveObjectWithData: seems ok");
Expand All @@ -70,17 +68,18 @@ int main()
"unarchiveObjectWithFile: seems ok");

// encode
data2 = [[NSMutableData alloc] initWithCapacity: 10240];
archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data2];
data2 = [NSMutableData dataWithCapacity: 10240];
archiver = AUTORELEASE([[NSKeyedArchiver alloc]
initForWritingWithMutableData: data2]);
[archiver encodeObject: val3 forKey: @"string"];
[archiver finishEncoding];

// decode...
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data2];
s = [[unarchiver decodeObjectForKey: @"string"] retain];
unarchiver = AUTORELEASE([[NSKeyedUnarchiver alloc]
initForReadingWithData: data2]);
s = [unarchiver decodeObjectForKey: @"string"];
PASS((s != nil && [s isKindOfClass:[NSString class]] && [s isEqual: val3]),
"encodeObject:forKey: seems okay");
[data2 release];

NSLog(@"Original string: %@, unarchived string: %@",val3, s);

Expand Down

0 comments on commit 87c5083

Please sign in to comment.