簡體   English   中英

不了解內存分析

[英]Don't understand memory analysis

我已將XCode升級到versio 3.2.3,以在我的iPhone項目上支持iOS4。 我使用靜態分析器檢查了內存管理問題。

在我的例程中,我遇到以下問題:在向日歷中添加事件以使其具有狀態之后,我會生成用戶警報。

這運行良好,但是內存分析器不喜歡我定義警報的方式。 我看不到編碼問題,對嗎? (我用“ <<<<”表示內存分析器提示)

- (IBAction) addToCalendar {
        ...
    UIAlertView  *tmpAlert  = [UIAlertView alloc];        <<<<Method returns an Objective-C object with a+1 retain count (owning reference)

    calData.startDate   = iVar.zeitVon;
    calData.endDate     = iEvent.zeitBis;
    calData.title       = iVar.title;
    calData.calendar    = myEventStore.defaultCalendarForNewEvents;

    if ([tmpEventStore saveEvent:tmpEvent span:EKSpanThisEvent error:&tmpSaveError]) {
        // Show a save success dialog
        [tmpAlert initWithTitle:@"Success"        <<<<Object released
                        message:@"entry saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    } else {
        // Show a save error dialog
        [tmpAlert initWithTitle:@"Error" 
                        message:@"entry not saved" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] ;
    }
    [tmpAlert show];                               <<<<Reference counted object is used after its released
    [tmpAlert release];
}

謝謝

您永遠不要解耦allocinit init經常會改變后台的對象! 嘗試

NSString* foo=[NSString alloc]; 
NSLog(@"%p %@", foo, [foo class]);
foo=[foo initWithString:@"bar"]; 
NSLog(@"%p %@", foo, [foo class]);

您會看到類似

2010-07-14 01:00:55.359 a.out[17862:903] 0x10010d080 NSPlaceholderString
2010-07-14 01:00:55.363 a.out[17862:903] 0x100001070 NSCFString

這表明+[NSString alloc]並沒有真正分配任何東西。 相反,工作是initWithString本身。 我不認為UIAlertView會這樣做,但您永遠不會知道。

回顧一下:永遠不要解耦allocinit 我認為靜態分析器只是假設每個人都使用[[... alloc] init] ,因此它被您的代碼弄糊塗了。 分析器應該警告您不要解耦allocinit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM