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

まず、メモリ リークに関してシミュレータが通知する内容はすべて無視できます。これは、異なるメモリ管理を使用しており、いくつかの誤検知を引き起こすことが知られています。

第二に、漏れはどのくらいの大きさですか?コードがスタック トレースに表示されず、リークが小さい場合は無視してください。Apple ライブラリのリークである可能性は低いか、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 に答える