Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BollywooD

macrumors 6502
Original poster
Apr 27, 2005
374
47
Hamburg
I am maintaing an open source project, and fired up Instruments to chesk it out, and found a leak in the following method - but cant for the life of me, pipoint it. Can anyone see where it might be.

thanks in advance:

Code:
- (NSArray *) cookies
{
	if (_children == nil)
	{
		// cookies -> cookieBuckets
		NSHTTPCookieStorage * cs = [NSHTTPCookieStorage sharedHTTPCookieStorage];
		NSArray * cookies = [cs cookies];
		NSMutableDictionary * cookieBuckets = [NSMutableDictionary dictionary];
		NSHTTPCookie * cookie;
		for (cookie in cookies)
		{
			NSString * domain = [cookie domain];

			NSString * site = [[self class] _siteWithDomainName:domain];
			if (site == nil)
				continue;
			NSMutableArray * siteCookies = [cookieBuckets objectForKey:site];
			if (siteCookies == nil)
			{
				siteCookies = [NSMutableArray array];
				[cookieBuckets setObject:siteCookies forKey:site];
			}
			[siteCookies addObject:cookie];
		}
		
		//get favorites from Favorites.plist
		NSString *applicationSupportFolder = [SCApplicationSupportFolderPath stringByExpandingTildeInPath];
		NSString *favoriteDomainsPlistPath = [applicationSupportFolder stringByAppendingPathComponent:SCFavoriteDomainsPlistFullName];
		NSDictionary* favoritesDictionary = [NSDictionary dictionaryWithContentsOfFile:favoriteDomainsPlistPath];
		NSMutableArray* favoriteDomains = [favoritesDictionary objectForKey:@"Domains"];
		NSMutableSet * unusedDomainSet = [NSMutableSet setWithArray:favoriteDomains];

		// cookieBuckets -> contentArray
		_children = [NSMutableArray new];
		for (NSString *key in cookieBuckets)
		{
			BOOL isFavorite = [favoriteDomains containsObject:key];
			if (isFavorite)
				[unusedDomainSet removeObject:key];
				
			NSArray * siteCookies = [cookieBuckets objectForKey:key];
			NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
				siteCookies, @"cookies",
				key, @"domainOnly",
				[NSString stringWithFormat:@"%@ (%d)", key, [siteCookies count]], @"domain",
				key, @"searchDescription",
				[NSNumber numberWithBool:isFavorite], @"isFavorite",
				[NSNumber numberWithBool:NO], @"isLeaf",
				nil];
		
			[(NSMutableArray *)_children addObject:dict];
		}
	
		NSString * domain;
		for (domain in unusedDomainSet)
		{
			NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
				[NSArray array], @"cookies",
				domain, @"domainOnly",
				[NSString stringWithFormat:@"%@ (%d)", domain, 0], @"domain",
				[NSNumber numberWithBool:YES], @"isFavorite",
				[NSNumber numberWithBool:YES], @"isLeaf",
				nil];
		
			[(NSMutableArray *)_children addObject:dict];
		}
	}	
	return _children;
}
 
I found it...... just needed a break from it......


I needed to release the array _children

I changed the last line from this:
Code:
return _children;

to this:
Code:
return [_children autorelease];
 
Don't use method names with a '_' or '__' prefix. Apple has specifically reserved that namespace in the docs, and you run the risk of overwriting important private methods that Apple is using behind the scenes with the potential for very bad consequences.
 
Don't use method names with a '_' or '__' prefix. Apple has specifically reserved that namespace in the docs, and you run the risk of overwriting important private methods that Apple is using behind the scenes with the potential for very bad consequences.

good point, thanks!:)
 
A C++ convention (don't know if people do this in Objective-C) is to start member variables with "m_." So it would be "m_Children."
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.