Fwd: Pb with object release
Fwd: Pb with object release
- Subject: Fwd: Pb with object release
- From: Simon Brée <email@hidden>
- Date: Wed, 9 Nov 2005 18:59:37 +0100
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.
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--
Ok Sherm, I understand now.
If I want to control the release of my image, I should then change my
code in :
NSImage *theDirIcon = [[NSImage alloc] init];
[[NSWorkspace sharedWorkspace] iconForFile:theDirLocation];
But in that case, the problem become the same with my NSString : I'm
losing the reference of my initial NSImage.
So how come, there is not a method for NSImage of type initWithImage,
like for NSString initWithString ?
How could I do without it ?
BTW, is there a way to check the reference couter of a particular
object in the debugger for exemple ? With ObjectAlloc, you can only
check the number of instance of a class.
Thanks,
Simon
_______________________________________________
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