【发布时间】:2011-01-16 11:25:39
【问题描述】:
看来我不了解 Objective C 中的内存管理...唉。
我有以下代码(请注意,在我的例子中,placemark.thoroughfare 和 placemark.subThoroughfare 都填充了有效数据,因此 if-conditions 将是 TRUE
item 绑定到 ManagedObjectContext。 item 中的托管变量(例如 place)具有使用 @dynamic 创建的 setter/getter。因此,声明是
@property (nonatomic, retain) NSString *place;
@dynamic place;
稍后在代码中,在 ReverseGeocoderDelegate 中,我访问它:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
if (placemark.thoroughfare) {
[item.place release];
item.place = [NSString stringWithFormat:@"%@ ", placemark.thoroughfare];
} else {
[item.place release];
item.place = @"Unknown Place";
}
if (placemark.thoroughfare && placemark.subThoroughfare) {
// *** problem is here ***
[item.place release];
item.place = [NSString stringWithFormat:@"%@ %@", placemark.thoroughfare , placemark.subThoroughfare];
}
如果我没有在代码中的标记位置释放item.place,Instruments 会发现那里存在内存泄漏。如果我这样做了,一旦我尝试在违规方法之外访问item.place,程序就会崩溃。
有什么想法吗?
【问题讨论】:
-
粘贴
place属性的声明以及它的setter方法的定义(如果有的话)会很有用。 -
你的意思是你用
@synthesize生成你的getter/setter? -
我使用了@dynamic,据我所知,这对于与 ManagedObjectContext 绑定的属性来说是正确的,就像我的情况一样
标签: objective-c ios memory-leaks autorelease convenience-methods