views:

31

answers:

1

Hello All,

So I was wondering if this was a common problem to be leaking memory using NSData to store a connection reponse data. For example, I have this line to initialize my data object

davData = [[NSMutableData data] retain];

And then when the connection errors or finishes loading, I release it. From my understanding, that should not be a leak correct? For some reason, the data is still showing up in instruments even after I release it. Any idea what could be causing this? Thanks!

A: 

Try initializing it like this:

davData = [[NSMutableData alloc] init];

The way you are doing it does not really create a leak, but it is dependent on your object being autoreleased by the system which is probably why you still see it.

Ben
The only problem with that is it crashes when trying to append the data later on. I assume it gets autoreleased from the pool at some point before that... the opposite problem I am having now.
Geoff Baum
The apple docs say that that should be retained as well. I am just not sure how to release it properly so it doesn't stick around. I suspect it happens when I initialize an XML parser with that data when the connection finishes loading...
Geoff Baum
As long as you alloc it then it won't be autoreleased, you could try releasing it upon completion of the xml parsing.
Ben
Ok I understand now. Well I have isolated it down to this line: [davData appendData:data]; I do that when the data is received. That is the object that is not being released.
Geoff Baum
Its pretty much up to the system when it actually calls the dealloc method, so if you release it after the XML parser has finished and it still shows up in instruments then there is not much you can do. It will be dealloc'd at some point in the runloop assuming its reference count is zero.
Ben
Ahhhh, I see. Well that makes sense. That's why it was still there, but not leaking. Well thanks for helping me understand this man!
Geoff Baum