Iphone – Instruments memory leak iphone

instrumentsiphonememory-leaks

I posted this problem a few days ago but it was very muddled and my question wasnt very clear so I removed it. I've been digging around and the memory leak is still persiting. Hopefully this attempt will be clearer.

First off I've run the static analyzer and it reports no memory leaks.

I then ran Instruments and it pointed to a memory leak at this line of code. As far as I can see there is no memory leak.

featured=[[UILabel alloc]initWithFrame:CGRectMake(130,15, 200, 15)];
    //[featured setFont:[UIFont UIFontboldSystemFontOfSize:20]];
    featured.font = [UIFont boldSystemFontOfSize:20];
    featured.backgroundColor= [UIColor clearColor];
    featured.textColor=[UIColor blackColor];    
    featured.text= @"Featured Promo";


    [self.view addSubview:featured];
    [featured release];
    featured=nil;

If I comment out the above code Instruments reports another memory leak in another block of code where there is no discernible leak.

UIButton   *populartbutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]];    
populartbutton.frame = CGRectMake(112, 145, 90, 22); // size and position of button
[populartbutton setTitle:@"Popular" forState:UIControlStateNormal];
populartbutton.backgroundColor = [UIColor clearColor];
populartbutton.adjustsImageWhenHighlighted = YES;   
[populartbutton addTarget:self action:@selector(getpopular:) 
       forControlEvents:UIControlEventTouchUpInside];


[self.view addSubview:populartbutton];

Instruments also says

Responsible Library = Core Graphics

Responsible Frame = open_handle_to_dylib_path

This Is the stack trace.


 53 Promo start
  52 Promo main /Users/..2/main.m:14
  51 UIKit UIApplicationMain
  50 UIKit -[UIApplication _run]
  49 CoreFoundation CFRunLoopRunInMode
  48 CoreFoundation CFRunLoopRunSpecific
  47 GraphicsServices PurpleEventCallback
  46 UIKit _UIApplicationHandleEvent
  45 UIKit -[UIApplication sendEvent:]
  44 UIKit -[UIApplication handleEvent:withNewEvent:]
  43 UIKit -[UIApplication _reportAppLaunchFinished]
  42 QuartzCore CA::Transaction::commit()
  41 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
  40 QuartzCore CALayerLayoutIfNeeded
  39 QuartzCore -[CALayer layoutSublayers]
  38 UIKit -[UILayoutContainerView layoutSubviews]
  37 UIKit -[UINavigationController _startDeferredTransitionIfNeeded]
  36 UIKit -[UINavigationController _startTransition:fromViewController:toViewController:]
  35 UIKit -[UINavigationController _layoutViewController:]
  34 UIKit -[UINavigationController_computeAndApplyScrollContentInsetDeltaForViewController:]

  33 UIKit -[UIViewController contentScrollView]
  32 UIKit -[UIViewController view]
  31 Promo -[FeaturedLevelViewController viewDidLoad] /Users/..s/FeaturedLevelViewController.m:67  // THIS IS MY CLASS WHERE THE CODE SAMPLES ABOVE ARE FROM


  30 UIKit -[UILabel initWithFrame:]
  29 UIKit -[UILabel _commonInit]
  28 UIKit +[UILabel defaultFont]
  27 UIKit +[UIFont systemFontOfSize:]
  26 GraphicsServices GSFontCreateWithName
  25 CoreGraphics CGFontCreateWithName
  24 CoreGraphics CGFontCreateWithFontName
  23 CoreGraphics CGFontFinderGetDefault
  22 CoreGraphics CGFontGetVTable
  21 libSystem.B.dylib pthread_once
  20 CoreGraphics load_vtable
  19 CoreGraphics load_library
  18 CoreGraphics CGLibraryLoadFunction
  17 CoreGraphics load_function
  16 CoreGraphics open_handle_to_dylib_path
  15 libSystem.B.dylib dlopen
  14 dyld dlopen
  13 dyld dyld::link(ImageLoader*, bool, ImageLoader::RPathChain const&)

  12 dyld ImageLoader::link(ImageLoader::LinkContext const&, bool, bool, ImageLoader::RPathChain const&)


  11 dyld ImageLoader::recursiveLoadLibraries(ImageLoader::LinkContext const&, bool, ImageLoader::RPathChain const&)


  10 dyld dyld::libraryLocator(char const*, bool, char const*, ImageLoader::RPathChain const*)


   9 dyld dyld::load(char const*, dyld::LoadContext const&)

   8 dyld dyld::loadPhase0(char const*, dyld::LoadContext const&, std::vector<char const*, std::allocator<char const*> >*)


   7 dyld dyld::loadPhase1(char const*, dyld::LoadContext const&, std::vector<char const*, std::allocator<char const*> >*)


   6 dyld dyld::loadPhase3(char const*, dyld::LoadContext const&, std::vector<char const*, std::allocator<char const*> >*)

   5 dyld dyld::loadPhase4(char const*, dyld::LoadContext const&, std::vector<char const*, std::allocator<char const*> >*)

   4 dyld dyld::loadPhase5(char const*, dyld::LoadContext const&, std::vector<char const*, std::allocator<char const*> >*)


   3 dyld dyld::mkstringf(char const*, ...)
   2 dyld strdup
   1 dyld malloc
   0 libSystem.B.dylib malloc

I'm really not too sure how to use this information to fix the problem so any guidance would be appreciated. Perhaps the answer is in the trace but I just don't know what to look for?

EDIT::

The above stack trace is when running on the simulator. The following is from running on a device. This trace does not point to any of my own classes

 23 Promo 0x0
  22 libSystem.B.dylib _pthread_body
  21 Foundation __NSThread__main__
  20 Foundation +[NSThread exit]
  19 libSystem.B.dylib _pthread_exit
  18 libSystem.B.dylib _pthread_tsd_cleanup
  17 QuartzCore CA::Transaction::release_thread(void*)
  16 QuartzCore CA::Transaction::commit()
  15 QuartzCore CA::Context::commit_transaction(CA::Transaction*)
  14 QuartzCore CALayerDisplayIfNeeded
  13 QuartzCore -[CALayer display]
  12 QuartzCore -[CALayer _display]
  11 QuartzCore CABackingStoreUpdate
  10 QuartzCore backing_callback(CGContext*, void*)
   9 QuartzCore -[CALayer drawInContext:]
   8 UIKit -[UIView(CALayerDelegate) drawLayer:inContext:]
   7 UIKit -[UILabel drawRect:]
   6 UIKit -[UILabel drawTextInRect:]
   5 UIKit -[UILabel _drawTextInRect:baselineCalculationOnly:]
   4 UIKit -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:lineBreakMode:]
   3 UIKit -[NSString(UIStringDrawing) drawAtPoint:forWidth:withFont:lineBreakMode:letterSpacing:includeEmoji:]
   2 WebCore WKSetCurrentGraphicsContext
   1 WebCore CurrentThreadContext()
   0 libSystem.B.dylib calloc

EDIT 2::

I noticed a leak that is reported earlier in the apps life cycle. It is pointed to this code block.

This is x-code generated stuff when you make a project right?

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

Best Answer

Firstly, you can ignore anything that the simulator tells you regarding memory leaks - it uses different memory management and is known to bring up some false positives.

Secondly, how big is the leak? If your code is not appearing in the stack trace and the leak is small, ignore it - it's either a leak in some apple library (unlikely) or is Instruments getting confused with frameworks. Either way, there's nothing much you can do about it.

In your case, it's looking like it's something to do with loading a UIFont; the data for these will probably be shared across applications anyway so it's likely to be safe to ignore!

As far as your snippets of code go, there are no obvious leaks that I can see :)

Hope that helps,

Sam