-2

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

enter image description here

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

self.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

Community
  • 1
  • 1
Spring
  • 11,333
  • 29
  • 116
  • 185
  • Please do NOT repost the same question: [iPhone SDK:How can I fix this leakage?](http://stackoverflow.com/questions/7015052/iphone-sdkhow-can-i-fix-this-leakage). Rather update your original question, comment on answers, or just wait for a better answer. After some time, you can consider placing a bounty if you still don't have a satisfactory answer. – PengOne Aug 10 '11 at 18:27
  • @PengOne are you not capable of seeing the main leakage problem and the screen shots are totally different here? or you just fooling around to downvote someone? – Spring Aug 10 '11 at 18:33
  • given that the question is the exact same title, the code is very similar, i'm not surprised people are downvoting and voting to close as duplicate. – John Gardner Aug 10 '11 at 18:37
  • @XDeveloper I think you shouldn't be so rude - you create a question with two questions in it, and your description of the leak is exactly the same as in last time - only screens are vary. – VMAtm Aug 10 '11 at 18:50
  • @VMAtm if you need to be kind you may have ask me to edit my question to include one question and change the title, rather than dowvoting and closing, which creates a garbage here – Spring Aug 10 '11 at 19:07
  • @XDeveloper You can easily do this even now, and flag your post to the moderator attention, and if he decide what your question is not duplicate, he will reopen it. – VMAtm Aug 10 '11 at 19:09

1 Answers1

4

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.

csano
  • 13,266
  • 2
  • 28
  • 45
  • yes exactly autorelase works and solves a leakege but why do I do that if dellaoc is eneough? – Spring Aug 10 '11 at 18:39
  • and what about the main problem? – Spring Aug 10 '11 at 18:39
  • A dealloc wouldn't have been enough in this case given that you had an object that had been over-retained. By not releasing [self.lastLoadedResponseXMLData copy] in the scope that it was copied, you end up with a retain count of 2, and while you are correct in releasing it in your dealloc method, you only release it once, which means you've still got one reference hanging around. The correct way to do this would have been to autorelease the copy and then release the reference that the property is holding onto in the dealloc method. – csano Aug 10 '11 at 19:21