不懂内存分析

发布于 2024-09-09 18:59:25 字数 1300 浏览 3 评论 0原文

我已将 XCode 升级到版本 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];
}

谢谢

I have upgraded my XCode to versio 3.2.3 to support iOS4 on my iphone project. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2024-09-16 18:59:25

您永远不应该将 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

You should never decouple alloc and init. init often changes the object behind the scenes! 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; rather, what does the job is initWithString itself. I don't think UIAlertView does this, but you never know.

To recap: never decouple alloc and init. I think the static analyzer just assumes that everyone use [[... alloc] init], so that it got confused by your code. The analyzer should have warned you not to decouple alloc and init.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文