views:

69

answers:

4

I know that when i have a property for an object which is set to retain or copy i must do the following.

Object *o = [[Object alloc] init];
self.myObject = o;
[o release];

but what if i don't define a property for myObject?

Can i do the following without having a leak?

Is it better to define a property for every variable I have in every class? Why??

myObject = [[Object alloc] init];
+1  A: 

What you can do is ensure you release the previously allocated object before assigning the variable holding it to a new instance (be it a retained version of another object, or a copy of it).

If you don't define a property for it, don't use the dot syntax.

jer
+1  A: 

Because Jacob already answered the part of without property. I will answer the left

Can i do the following without having a leak?

myObject = [[Object alloc] init];

You have to release the myObject somewhere, maybe in the delegate method, or maybe after you finish using it, but remember to release the object, especially before you lose the object reference

Is it better to define a property for every variable I have in every class? Why??

It depends on your coding convention, define all properties will help you with memory management a lot, but it comes with burden and sometimes your team doesn't want to define instance variable, property and synthesize for every variable, which means quite a lot of codes. So, discussing with your team to see what is your weakness and strength. If you are weak at memory management, I recommend to define for every

vodkhang
OR you could use `new` instead of `alloc init` :)
Matt S.
ah yeah, you can use new
vodkhang
+1  A: 

You don't need to have any properties. Obj-C didn't even have them at all until a couple of years ago with Obj-C 2. Properties are a convenience - they give you some memory management without you writing boilerplate code (the access methods are synthesized for you), and you can use dot notation, which some people are more familiar with than the usual Obj-C syntax.

Having said that properties take care of memory management for you, that's only partially true. You still need to set all properties to nil in your dealloc method. So that really doesn't buy you much, since without properties you'd still need to release each object in dealloc.

The big benefit is that properties handle reference counting for you when assigning objects from one place to another. You don't have to write the getters and setters manually. For this reason, I like properties. Plus, clang is gaining the ability to have properties where you only need the @property line, no need for a backing variable, and no need for an @synthesize line. One-line properties are a win in my book. (OK, two lines, you need to add a line to dealloc too :)

Graham Perks
+2  A: 

Yes, you can set an instance variable that way and it's totally fine — in fact, that's the preferred way to do it in init methods (because setters might have side effects). Just remember to release the old object (if any) in the variable before setting a new one or you'll leak. And whether you use properties or set the ivar directly, you need to release the object in dealloc.

Chuck