2

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

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

然后我运行 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;

如果我注释掉上面的代码 Instruments 会在另一个没有明显泄漏的代码块中报告另一个内存泄漏。

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

仪器还说

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

负责框架 = 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

我真的不太确定如何使用这些信息来解决问题,所以任何指导都将不胜感激。也许答案就在跟踪中,但我只是不知道要寻找什么?

编辑::

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

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

2 回答 2

6

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

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

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

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

希望有帮助,

山姆

于 2010-05-05T14:44:48.207 回答
0

尝试更改此行:

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

对此:

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

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

于 2010-05-05T14:06:18.303 回答