不懂内存分析
我已将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不应该将
alloc
和init
解耦。init
经常在幕后更改对象!尝试你会看到类似的内容
这表明
+[NSString alloc]
并没有真正分配任何东西;相反,该工作是 initWithString 本身。我不认为UIAlertView
会这样做,但你永远不知道。回顾一下:永远不要将
alloc
和init
解耦。我认为静态分析器只是假设每个人都使用[[... alloc] init]
,因此它会被您的代码混淆。分析器应该警告您不要解耦alloc
和init
。You should never decouple
alloc
andinit
.init
often changes the object behind the scenes! TryYou'll see something like
This shows that
+[NSString alloc]
doesn't really allocate anything; rather, what does the job isinitWithString
itself. I don't thinkUIAlertView
does this, but you never know.To recap: never decouple
alloc
andinit
. 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 decouplealloc
andinit
.