Re: Memory management question.
Re: Memory management question.
- Subject: Re: Memory management question.
- From: Pandaa <email@hidden>
- Date: Fri, 25 Mar 2005 11:03:22 +0100
2005-03-25 kl. 09.43 skrev Scott Andrew:
I am just learning (making the jump form C++ and windows land) and I
have a
question..
Everywhere I read it talks about having to release stuff that you
alloc or
using autorelease to have it cleaned up automatically.
Yes, but in your code example it is + dataWithData: that allocates the
"body" NSData object and so the responsibility to release it lies with
+ dataWithData: and not your method.
So I am relesaing my string I use to create the data for the body. Why
don't
I have to release my request or body? When I do the app crashes.
According to MallocDebug and leaks nothing is leaking. So I am just
curious
about this.
Because [NSData dataWithData:..] returns an >> already autoreleased <<
NSData instance (and sendSynchronousRequest:returningResponse:error:
also returns an autoreleased NSURLResponse instance though this could
be stated more clearly in the docs ).
If you do release the returned NSData instance it will be deallocated
immediately, and there will be an error when the autorelease-pool is
later emptied and attempts to release the instance again.
The convention here is roughly to have convenience methods always
return autoreleased instances unless they're named "copy" "alloc" or
"new" something.
Memory management / object ownership in Cocoa is simple once you
understand it, see, e.g.:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. email@hidden . . www.synapticpulse.net .
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden