iphone - 仪器内存泄漏 iPhone

标签 iphone memory-leaks instruments

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

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

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

仪器也说

责任库 = Core Graphics

责任框架= 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;
}

最佳答案

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

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

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

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

希望有帮助,

山姆

关于iphone - 仪器内存泄漏 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2773836/

相关文章:

iphone - iPhone 开发的卷页和翻页效果

ios - 相机预览黑色

iphone - 如何在appdelegate上检查横向和纵向模式?

linux - 执行并发 os/exec.Command.Wait() 时发生内存泄漏

c++ - 如果程序在调试时有内存损坏,但在发布时运行良好,是否可以?

objective-c - 使用仪器泄漏和对象分配 : Are autoreleased objects counted as leaks?

iphone - ios double 来自没有四舍五入的字符串

xcode - High Sierra 中的 NSCollectionView 内存泄漏?

ios - 是否有一个 Instruments Instrument 可以记录我应用程序中的所有调用

Xcode 仪器。虚拟内存是什么意思? iPhone OS 没有虚拟机?