Quote:
Originally Posted by MatuX
Thanks a lot for all of your responses!
Common sense dictated to me that since I wasn't retaining the object I just allocated (and I was giving it directly to another object), the retain count wouldn't increase.
Does this means that if I do [obj setSomething:[[OtherObj alloc] init]; it will automatically become a leak? If it is so, why the compiler doesn't signal a warning here?
So the rule is that every time I do "alloc", the retain count increases 1 and I am the one responsible to decrease it?
Edit:
If that is the rule, isn't there a way to stop the language from doing this? It will make the code really messy if I can't do something like:
Code:
[myObj setThis:[[OtherObj alloc] init];
and I'm forced to add these redundant lines:
Code:
OtherObj otherObj = [[OtherObj alloc] init];
[myObj setThis:otherObj];
[otherObj release];
------
And what if the property is set to assign instead of retain? When I do "release" I will render the allocated memory unusable?
Must I check for every property if its set to assign or retain every time I use it?
|
That's what autorelease is for:
Code:
[myObj setThis:[[[OtherObj alloc] init] autorelease];
You created it, you're making sure it gets released. AFTER the other object has a chance to retain it if needed. Just make sure that myObj DOES retain it if needed.
Also, the compiler can't give a warning because it doesn't know what [myObj setThis:] is going to do.
joe