FROM : John Tsombakos
DATE : Fri Jan 24 23:46:08 2003
On Saturday, January 25, 2003, at 01:52 AM, Shawn Erickson wrote:
>
> 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.
>
That in concept I understand.
> 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
>
Yup. Read those...
>> 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.
>
This is the explanation that I needed to understand what is going on...
thanks! I guess I didn't make the connection that
resourceDataUsingCache allocated the object and I didn't need to do
anything with it.
>> [[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.
>
I did figure out this one.
> 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).
>
I did NSLog the retainCount of imageData and did see that it went to 2
after this line. Now it makes sense why.
This is very helpful too..
>> 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.
>
This I got to. The best piece of advice I read somewhere, and I wish I
could remember where, is if the method has the class name in it, it's
most likely a convenience constructor, and the returned object is
autoreleased.
> -Shawn
Thanks! Now I just have to figure out how to work around the partial
loading of the image and the "Application transferred too few
scanlines" message.
john
DATE : Fri Jan 24 23:46:08 2003
On Saturday, January 25, 2003, at 01:52 AM, Shawn Erickson wrote:
>
> 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.
>
That in concept I understand.
> 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
>
Yup. Read those...
>> 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.
>
This is the explanation that I needed to understand what is going on...
thanks! I guess I didn't make the connection that
resourceDataUsingCache allocated the object and I didn't need to do
anything with it.
>> [[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.
>
I did figure out this one.
> 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).
>
I did NSLog the retainCount of imageData and did see that it went to 2
after this line. Now it makes sense why.
This is very helpful too..
>> 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.
>
This I got to. The best piece of advice I read somewhere, and I wish I
could remember where, is if the method has the class name in it, it's
most likely a convenience constructor, and the returned object is
autoreleased.
> -Shawn
Thanks! Now I just have to figure out how to work around the partial
loading of the image and the "Application transferred too few
scanlines" message.
john
| 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

