Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 06-04-2009, 09:10 PM   #7 (permalink)
FlyingDiver
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by MatuX View Post
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
FlyingDiver is offline   Reply With Quote
 
Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,064
Threads: 38,852
Posts: 170,452
Top Poster: smasher (2,563)
Welcome to our newest member, HenLeop934
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:42 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.