iPhone SDK:我该如何解决这个漏洞? [英] iPhone SDK:How can I fix this leakage?
问题描述
我有一个leakege在这里但是找不到问题;
I have a leakege here but couldnt find the problem;
@property (nonatomic,retain) NSMutableData *responseXMLData;
@property (nonatomic,copy) NSMutableData *lastLoadedResponseXMLData;
-(void)dealloc {
[doc release];
doc=nil;
[xmlBodyTemp release];
[responseXMLData release] ;
responseXMLData=nil;
[lastLoadedResponseXMLData release];
lastLoadedResponseXMLData=nil;
[xmlBody release];
[super dealloc];
}
推荐答案
没有看到你的 dealloc
方法,我们无法确定您是否正确发布了这些属性的值。
Without seeing your dealloc
method, we cannot be sure you are correctly releasing the values of these properties.
但是在发布的代码中,我做了看到一个主要问题。但这不是你想的那样。
But in the posted code, I do see one major problem. But it's not where you think.
self.lastLoadedResponseXMLData = docTempData;
此行虽然由XCode标记,但仍然可以(假设您在<$ c中正确释放了值) $ c> dealloc )。
This line, although flagged by XCode, is fine (assuming you release the value correctly in dealloc
).
self.responseXMLData = [self.lastLoadedResponseXMLData copy];
然而,这一行并不合适。它会复制 self.lastLoadedResponseXMLData
中的任何值,但由于副本,您永远不会释放引用。 self.responseXMLData
,因为它被声明为retain,添加了自己对该对象的引用,并且(假设您在 dealloc <中正确释放了值/ code>)这个引用是清理过的。
This line, however, is not fine. It makes a copy of whatever value is in self.lastLoadedResponseXMLData
but you never release the reference due to the copy. self.responseXMLData
, since it is declared "retain", adds its own reference to the object, and (assuming you release the value correctly in dealloc
) this reference is the one cleaned up.
如果你真的不需要关心对象是相同的还是副本,只是放弃了副本。否则,自动释放它:
If you don't really need to care whether the object is the same or is a copy, just forgo the copy. Otherwise, autorelease it:
self.responseXMLData = [[self.lastLoadedResponseXMLData copy] autorelease];
这篇关于iPhone SDK:我该如何解决这个漏洞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!