Leaks in main.m?

Discussion in 'iOS Programming' started by zerocustom1989, Jul 10, 2010.

  1. zerocustom1989 macrumors regular

    Sep 5, 2007
    So im new to using instruments, but It seems to be doing a great job at telling me I have a ton of leaks in my main.m function while testing on a device.

    How do I go about tracking these leaks down? I dont know know how to reasonably follow these stack traces. They all go into the foundation stuff.

    Any advice? Here's a stack trace ex:

    34 GPA start
    33 GPA main /Users/johndavis/iPhone Apps/GPA/main.m:14
    32 UIKit UIApplicationMain
    31 UIKit -[UIApplication _run]
    30 GraphicsServices GSEventRun
    29 GraphicsServices GSEventRunModal
    28 CoreFoundation CFRunLoopRunInMode
    27 CoreFoundation CFRunLoopRunSpecific
    26 CoreFoundation __CFRunLoopRun
    25 CoreFoundation __CFRunLoopDoSources0
    23 SystemConfiguration rlsPerform
    22 AppSupport _ReachabilityCallback
    21 AppSupport -[_ReachabilityRequest reachabilityChangedWithFlags:]
    20 CoreFoundation CFDictionaryApplyFunction
    19 CoreFoundation CFBasicHashApply
    18 CoreFoundation __CFDictionaryApplyFunction_block_invoke_1
    17 AppSupport _NotifyObserver
    16 CoreFoundation -[NSObject(NSObject) performSelector:withObject:]
    15 iAd -[ADManager _reachabilityChanged:]
    14 CoreFoundation -[NSArray makeObjectsPerformSelector:]
    13 CoreFoundation -[NSObject(NSObject) performSelector:]
    12 iAd -[ADCache reachabilityChanged]
    11 iAd -[ADCache _considerFetchingMore]
    10 iAd -[ADCache _performFetch]
    9 iAd -[ADManager propertiesRequest]
    8 iAd -[ADManagerInternal mobileCountryCode]
    7 CoreTelephony -[CTTelephonyNetworkInfo init]
    6 CoreTelephony -[CTTelephonyNetworkInfo updateNetworkInfoAndShouldNotifyClient:]
    5 Foundation -[NSCFString copyWithZone:]
    4 CoreFoundation CFStringCreateCopy
    3 CoreFoundation __CFStringCreateImmutableFunnel3
    2 CoreFoundation _CFRuntimeCreateInstance
    1 CoreFoundation CFAllocatorAllocate
    0 CoreFoundation __CFAllocatorSystemAllocate
  2. zerocustom1989 thread starter macrumors regular

    Sep 5, 2007
    If I can't fix my leaks and they dont cause the app to crash or have performance issues will my App be accepted into the store if its not denied for other reasons?
  3. HarryWorksInc macrumors regular

    Feb 21, 2010
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Few comments.

    This leak isn't in main. The leak tool tells you where the leaked object is created, not where it leaked. Also this leaked string obviously wasn't created in main either. It was created with a stack trace over 30 methods away from main.

    This seems to be related to iAd. It's still quite possible that the leak is due to bugs in your code related to iAd. You should look at the history of the string in the leaks tool and see if it's been retained in your code. Also, because of the nature of cocoa apps where lots of container objects are used often only a single leaked container, which could be an array, dictionary, or UIView, results in the leak of ten other objects. Fix the one leak and all the other leaks are fixed too.
  5. zerocustom1989 thread starter macrumors regular

    Sep 5, 2007
    Ok, I've decided to narrow down my search and ignore the iAd related one and first look at one of my earlier leaks. The one I want to tackle has the responsible frame: [UINavigationController initWithCoder]. It says the origin function is the main function, and doesn't provide any other .m files down the line. Can I say that this means that my delegate function or mainWindow.xib are to blame? I just find it odd that the problem wouldnt be in one of the files that I created cause I started with a template.
  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    What kind of object is leaked? Where is it retained?
  7. zerocustom1989 thread starter macrumors regular

    Sep 5, 2007
    Ok, I did some reading and contacted a few friends of mine regarding the nature of leaks just to make sure my understanding of what they are is correct.

    I think I'm still a little shaky on what they are in the context of what im seeing, but on to this navigation controller leak. It seems to have disappeared... I didnt really 'do' anything to prevent it from appearing though...

    I'm going to chalk that up to being 'weird' and move onto a more obvious leak that i've discovered dealing with core Data. This one is straight forward for me to see.

    I create a coreData Entity and push it into a modalController. When pressing cancel instead of save though I get a leak even though I delete the entity inside the cancel button.

    So here's what I have:
    Stack Trace:
    23 GPA start
    22 GPA main /Users/johndavis/iPhone Apps/GPA/main.m:14
    21 UIKit UIApplicationMain
    20 GraphicsServices GSEventRun
    19 GraphicsServices GSEventRunModal
    18 CoreFoundation CFRunLoopRunInMode
    17 CoreFoundation CFRunLoopRunSpecific
    16 CoreFoundation __CFRunLoopRun
    15 CoreFoundation __CFRunLoopDoSource1
    13 GraphicsServices PurpleEventCallback
    12 UIKit _UIApplicationHandleEvent
    11 UIKit -[UIApplication sendEvent:]
    10 UIKit -[UIWindow _sendTouchesForEvent:]
    9 UIKit -[UIControl touchesEnded:withEvent:]
    8 UIKit -[UIControl(Internal) _sendActionsForEvents:withEvent:]
    7 UIKit -[UIControl sendAction:to:forEvent:]
    6 UIKit -[UIApplication sendAction:to:from:forEvent:]
    5 UIKit -[UIActionSheet(Private) _buttonClicked:]
    4 UIKit -[UIActionSheet dismissWithClickedButtonIndex:animated:]
    3 GPA -[RootViewController actionSheet:willDismissWithButtonIndex:] /Users/johndavis/iPhone Apps/GPA/Classes/RootViewController.m:616
    2 CoreData +[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]
    1 CoreData +[NSManagedObject(_PFDynamicAccessorsAndPropertySupport) allocWithEntity:]
    0 CoreData _PFAllocateObject


    I can see that this happens when I allocate with the following line of code within the following block of code:
    Course *newCourse = [NSEntityDescription insertNewObjectForEntityForName:@"Course" inManagedObjectContext:context];
    			CourseAddViewController *addCourseController = [[CourseAddViewController alloc] initWithNibName:@"CourseAddView" bundle:nil];
    			addCourseController.delegate = self;
    			NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    			Course *newCourse = [NSEntityDescription insertNewObjectForEntityForName:@"Course" inManagedObjectContext:context];
    			addCourseController.course = newCourse;
    			UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addCourseController];
    			[self presentModalViewController:navigationController animated:YES];
    			[navigationController release];
    			[addCourseController release];
    The leak occurs when cancel inside the modal controller is pressed:
    - (void)cancel {
    	[course.managedObjectContext deleteObject:course];
    	NSError *error = nil;
    	if (![course.managedObjectContext save:&error]) {
    		 Replace this implementation with code to handle the error appropriately.
    		 abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
    		NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        [self.delegate courseAddViewController:self didAddCourse:nil];
    See Now I would assume that im following proper memory management here? Am I not? I'm getting rid of the entity because im not saving it.

    Leaked Object: "Malloc 80 Bytes" followed by "Malloc 16 Bytes."

    I'm not sure where to find the information to answer "Where the object is maintained"

    I'm hoping if someone can help me with a leak or two I can get the hang of this. The iAds ones are still confusing me too.

Share This Page