开发者

iPhone SDK: how can fix this leakage? [duplicate]

This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

iPhone SDK:How can I fix this leakage?

This question is different from the previous one, even it is in the same class,

I have a leakage as below,

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

iPhone SDK: how can fix this leakage? [duplicate]

Second question: in the variables given above when I write:

se开发者_开发问答lf.responseXMLData = [self.lastLoadedResponseXMLData copy];

Do I need to release self.lastLoadedResponseXMLData once more other than I did in dellaoc? or only delloc is eneough? autorelease seems to work but didnt get the idea why


I highly recommend that you read through the Memory Management Programming Guide that Apple has provided. This will give you a better understanding of how to better manage your allocations.

--

IMO (not sure others will agree), all you need to do in dealloc is release your objects. There's no need to set them to nil.

You need to release the object that you're copying. When you copy[1] something, the retain count is incremented by 1. Then when you assign it to self.responseXMLData, the retain count is incremented by 1 again (since the property does a retain). The best thing to do here would be to autorelease it. [[self.lastLoadedResponseXMLData copy]autorelease];

[1] The simplest rule of thumb is that any time you alloc, copy, new, or retain something, you own it, and you're responsible for releasing it in the scope in which you took ownership.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜