仪器内存泄漏iphone

时间:2010-05-05 13:58:25

标签: iphone memory-leaks instruments

我几天前发布了这个问题,但它非常混乱,我的问题不是很清楚所以我删除了它。我一直在挖掘,内存泄漏仍然存在。希望这种尝试会更清楚。

首先,我运行静态分析器并报告没有内存泄漏。

然后我运行了仪器,它指出了这行代码中的内存泄漏。据我所知,没有内存泄漏。

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;

如果我注释掉上面的代码,仪器报告另一个代码块中的另一个内存泄漏,其中没有可识别的泄漏。

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];

乐器也说

负责任的图书馆=核心图形

Responsible Frame = open_handle_to_dylib_path

这是堆栈跟踪。


 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

我真的不太确定如何使用这些信息来解决问题,所以任何指导都会受到赞赏。也许答案就在于追踪,但我只是不知道该寻找什么?

EDIT ::

上面的堆栈跟踪是在模拟器上运行时。以下是在设备上运行。此跟踪不指向我自己的任何类

 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

编辑2 ::

我注意到应用程序生命周期早期报告的泄漏。它指向此代码块。

当你做一个项目时,这是x代码生成的东西吗?

#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;
}

2 个答案:

答案 0 :(得分:6)

首先,您可以忽略模拟器告诉您的有关内存泄漏的任何内容 - 它使用不同的内存管理,并且已知会出现一些误报。

其次,泄漏有多大?如果您的代码没有出现在堆栈跟踪中并且泄漏很小,请忽略它 - 它可能是某个苹果库中的泄漏(不太可能)或者是仪器与框架混淆。无论哪种方式,你都无能为力。

在你的情况下,看起来它与加载UIFont有关;无论如何,这些数据可能会在应用程序之间共享,因此忽略它可能是安全的!

就您的代码片段而言,我看不到明显的泄漏:)

希望有所帮助,

萨姆

答案 1 :(得分:0)

尝试更改此行:

featured=[[UILabel alloc]initWithFrame:CGRectMake(130,15, 200, 15)];

到此:

featured=[[[UILabel alloc]initWithFrame:CGRectMake(130,15, 200, 15)] autorelease];

我认为如果没有这个,我会得到额外的保留计数。