FROM : Shawn Erickson
DATE : Fri Jan 24 22:54:03 2003
On Friday, January 24, 2003, at 09:53 PM, John Tsombakos wrote:
> Second question deals with retain and release. I originally did not
> have the [image release] statement in there and while watching 'top',
> I could see that memory was being taken up and not released every time
> I clicked the button (assuming the image actually loaded). I added the
> release and the memory stopped being eaten up. I then figured I also
> needed to release the imageData object (marked **2). When that's in
> there, the app crashes. My question is why don't I need to release
> that? I understand the concept of the release and retain, but just
> can't figure out which routines return something that I need to
> release. Any hints or help would be nice.
Try to keep this in mind when working with Cocoa frameworks (and
hopefully follow it in your own code): If you alloc, copy or retain an
object then it is your responsibility to release it.
I suggest you read one or all of the following...
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
http://cocoadev.com/index.pl?MemoryManagement
http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/
ProgrammingTopics/MemoryMgmt/index.html
> NSData *imageData = [url resourceDataUsingCache:NO];
Here you are not allocating the data, resourceDataUsingCache is and it
is its responsibility to release the object. It also has to return the
object to you so you get a chance to use it, so it autoreleases it
before it returns.
> [[NSImage alloc] initWithData:imageData];
Here you are allocating a NSImage object so it is your responsibility
to release it when you are done with it.
As a side NSImage retains (or possibly copies) imageData so that
imageData stays around while NSImage needs it. When NSImage goes away
it will release imageData (sends it a release message).
> NSURL* url = [NSURL URLWithString:string];
One last note... the above is just like resourceDataUsingCache in that
URLWithString is creating a URL instance and so it must be responsible
for making sure it gets released.
-Shawn
DATE : Fri Jan 24 22:54:03 2003
On Friday, January 24, 2003, at 09:53 PM, John Tsombakos wrote:
> Second question deals with retain and release. I originally did not
> have the [image release] statement in there and while watching 'top',
> I could see that memory was being taken up and not released every time
> I clicked the button (assuming the image actually loaded). I added the
> release and the memory stopped being eaten up. I then figured I also
> needed to release the imageData object (marked **2). When that's in
> there, the app crashes. My question is why don't I need to release
> that? I understand the concept of the release and retain, but just
> can't figure out which routines return something that I need to
> release. Any hints or help would be nice.
Try to keep this in mind when working with Cocoa frameworks (and
hopefully follow it in your own code): If you alloc, copy or retain an
object then it is your responsibility to release it.
I suggest you read one or all of the following...
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
http://cocoadev.com/index.pl?MemoryManagement
http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/
ProgrammingTopics/MemoryMgmt/index.html
> NSData *imageData = [url resourceDataUsingCache:NO];
Here you are not allocating the data, resourceDataUsingCache is and it
is its responsibility to release the object. It also has to return the
object to you so you get a chance to use it, so it autoreleases it
before it returns.
> [[NSImage alloc] initWithData:imageData];
Here you are allocating a NSImage object so it is your responsibility
to release it when you are done with it.
As a side NSImage retains (or possibly copies) imageData so that
imageData stays around while NSImage needs it. When NSImage goes away
it will release imageData (sends it a release message).
> NSURL* url = [NSURL URLWithString:string];
One last note... the above is just like resourceDataUsingCache in that
URLWithString is creating a URL instance and so it must be responsible
for making sure it gets released.
-Shawn
| Related mails | Author | Date |
|---|---|---|
| John Tsombakos | Jan 24, 22:05 | |
| Shawn Erickson | Jan 24, 22:54 | |
| John Tsombakos | Jan 24, 23:46 | |
| Jeremy Erwin | Jan 25, 00:23 | |
| James J. Merkel | Jan 25, 11:45 | |
| Shawn Erickson | Jan 25, 12:43 | |
| Shawn Erickson | Jan 25, 13:01 | |
| James J. Merkel | Jan 25, 13:36 | |
| Jeremy Erwin | Jan 25, 13:42 | |
| Shawn Erickson | Jan 26, 08:56 |






Cocoa mail archive

