简体   繁体   English

不了解内存分析

[英]Don't understand memory analysis

I have upgraded my XCode to versio 3.2.3 to support iOS4 on my iphone project. 我已将XCode升级到versio 3.2.3,以在我的iPhone项目上支持iOS4。 using the static analyser I checked for memory management problems. 我使用静态分析器检查了内存管理问题。

In one of my routines I get the following problem: I generate a user alert after adding an event to the calendar to give him a status. 在我的例程中,我遇到以下问题:在向日历中添加事件以使其具有状态之后,我会生成用户警报。

This runs fine, but the memory analyser doesn't like how I defined the alert. 这运行良好,但是内存分析器不喜欢我定义警报的方式。 I can't see the coding problem, do you? 我看不到编码问题,对吗? (I indicated the memory analyser hints with "<<<<") (我用“ <<<<”表示内存分析器提示)

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

thanks 谢谢

You should never decouple alloc and init . 您永远不要解耦allocinit init often changes the object behind the scenes! init经常会改变后台的对象! Try 尝试

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

You'll see something like 您会看到类似

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

This shows that +[NSString alloc] doesn't really allocate anything; 这表明+[NSString alloc]并没有真正分配任何东西。 rather, what does the job is initWithString itself. 相反,工作是initWithString本身。 I don't think UIAlertView does this, but you never know. 我不认为UIAlertView会这样做,但您永远不会知道。

To recap: never decouple alloc and init . 回顾一下:永远不要解耦allocinit I think the static analyzer just assumes that everyone use [[... alloc] init] , so that it got confused by your code. 我认为静态分析器只是假设每个人都使用[[... alloc] init] ,因此它被您的代码弄糊涂了。 The analyzer should have warned you not to decouple alloc and init . 分析器应该警告您不要解耦allocinit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM