Memory Leak

Discussion in 'Mac Programming' started by BollywooD, Dec 29, 2009.

  1. BollywooD macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #1
    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;
    }
    
     
  2. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #2
    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];
     
  3. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    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.
     
  4. BollywooD thread starter macrumors 6502

    BollywooD

    Joined:
    Apr 27, 2005
    Location:
    Surfers Paradise
    #4
    good point, thanks!:)
     
  5. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #5
    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."
     

Share This Page