opensubscriber
   Find in this group all groups
 
Unknown more information…

o : objc-language@lists.apple.com 9 November 2005 • 6:32AM -0500

Re: Pb with object release
by Sherm Pendley

REPLY TO AUTHOR
 
REPLY TO GROUP



On Nov 8, 2005, at 5:02 PM, Simon Brée wrote:

> I don't understand what I'm doing wrong concerning memory management

The cocoa-dev list would be a better place to ask - memory management  
with retain/release is part of the Cocoa framework, not of the  
Objective-C language. Having said that...

> in my following code : when I try to uncomment one or both lines at  
> the end to release my objects, my apps exit with a signal 10.
>
> Do you see why ?
>
> Thanks in advance,
>
> Simon
>
> - (IBAction) setLocation:(id)sender {
> [...]
> if (theIndex == 2) {
> NSString *theDirLocation = [[NSString alloc] init];

What's the point of the line above? You're creating an empty string,  
but it's not a mutable string, so you can't change it.

> [...]

I have a hunch that somewhere in the above [...], you're doing  
something like this:

theDirLocation = [theDirLocation  
stringByAppendingPathComponent:@"foo.jpg"];

There are several important things to understand here. First is that  
methods like this one do *not* modify the original string; instead,  
they create a new one. Second, because the new string wasn't created  
with +alloc or -copy, you're not responsible for releasing it.

And finally, because you've assigned theDirLocation to point to the  
new object, you no longer have a reference to the old one.  
Unfortunately, the old one *was* created with +alloc, and it was  
therefore your responsibility to release it, so in addition to a  
crash you have a memory leak.

> NSImage *theDirIcon = [[NSWorkspace sharedWorkspace]  
> iconForFile:theDirLocation];

Note - theDirIcon was not created with +alloc or -copy, and you  
haven't sent it a -retain. So, you don't need to release it.

sherm--

Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (Objc-language@list...)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/objc-language/subscriber%40opensubscriber.com

This email sent to subscriber@open...

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

opensubscriber is not affiliated with the authors of this message nor responsible for its content.