1

I'm a little confused about synthesized properties. I have an array that I want to be accessible from other classes so this is my code:

MyClass.h

@interface MyClass : CCLayer {
    NSMutableArray *myArray;
}

@property (nonatomic, retain) NSMutableArray *myArray;

MyClass.m

@synthesize myArray;

-(id)init
{
    myArray = [[NSMutableArray alloc] init];
}

-(void)dealloc
{
    [myArray release];
    myArray = nil;
}

I am a little confused now..is myArray the same as self.myArray? Do I have to release self.myArray as well? Thanks.

Moo33
  • 1,251
  • 3
  • 15
  • 27
  • 1
    I assume that there is a good reason why you are not using ARC yet :) – Sergey Kalinichenko Oct 17 '12 at 09:33
  • As he is using cocos2d, I assume he want to create a game. I don't think that arc is good enough for it. – Morion Oct 17 '12 at 09:48
  • @Morion: where did you get that idea from? Of course ARC is perfectly fine for game development and cocos2d. It's generally faster, safer, you write less code that's generally more stable. You might want to read this: http://www.learn-cocos2d.com/2012/06/mythbusting-8-reasons-arc/ – CodeSmile Oct 17 '12 at 13:34
  • @LearnCocos2D: maybe I am a bit conservative, but I prefer to have control of creation and deleting of all of my objects. Especially for those who use textures and sounds, as it takes a lot of memory and sometimes I want to be sure that no one object that retains my texture will not try to use it if I unloaded it from memory manually. – Morion Oct 17 '12 at 13:50
  • @LearnCocos2D: thanks for link. Article is interesting, but I still has no irresistable desire to convert all of my projects to ARC =) It doesn't mean that I don't want to study. Just, as I mentioned before, I want to be sure, when my objects are alive and when they are deleted =) – Morion Oct 17 '12 at 14:24

5 Answers5

2

You declared your property as retain, it means that it will be retained automatically if you will set is using self.myArray. So, you can simply create autoreleased array in your init method and set it as

myArray = [NSMutableArray array];
self.myArray = myArray;

in this case you are not have to release it in the dealloc method or anything else. And as dasblinkenlight said you have to use @synthesize if you want to be sure that self.myArray is linked with your myArray instance.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • So I don't need to release anything at all? How about the array property I've retained? – Moo33 Oct 18 '12 at 03:51
1

Assuming that your @synthesize directive looks like this

@synthesize myArray;

you do not need to do anything in addition to what you are already doing: your property stores its value in the instance variable of the same name.

EDITED : Removed the alternative that suggests setting self.myArray in the dealloc method.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Yes you do, the best method is to set the property nil and release your variable.

-(void)dealloc{
    self.myArray = nil;
    [myArray release];

    [super dealloc];
}
Morion
  • 10,495
  • 1
  • 24
  • 33
btype
  • 1,593
  • 19
  • 32
0

The code you provided is not really correct.

No, accessing a property and accessing the field itself are not the same.

My guess is that you are looking at old obj C examples where it was necessary to create the field with the property.

You also have no @synthesize directive in your code.

In current obj C code there is no need to declare a field to back the property, the field and the getter and setter will be autosynthesized (generated by the compiler) for you.

The default field generation is the name of your property with an underscore in front of it.

When you access the field directly via _myArray you will bypass any retain or release code that is contained in the generated getter/setter and have to manually manage memory in a non ARC project.

So to sum up, you dont need your field definition, and you dont need a synthesize directive.

You access your field directly with _myArray, or the property via self.myArray

They are not the same thing, one goes through generated code which obeys your property definition as to retain, assign, copy and accessing the field directly bypasses these semantics altogether.

If you define your property as retain you will need to release it in dealloc

You can use either

self.myArray = nil;

which will handle the release or

[_myArray release];
_myArray = nil;

Although someone in a previous post said setting the property to nil in dealloc might cause a problem Ive never seen it actually happen in my apps, ymmv

deleted_user
  • 3,817
  • 1
  • 18
  • 27
  • Thanks for your answer! Yes I didn't know that you could set a property without the field. So judging from your comment is this code right? MyClass.h `@interface MyClass : CCLayer { } @property (nonatomic, retain) NSMutableArray *myArray;` MyClass.m `-(id)init { self.myArray = [NSMutableArray array]; } -(void)dealloc { self.myArray = nil; }` – Moo33 Oct 18 '12 at 03:59
0

To answer your questions:

I am a little confused now..is myArray the same as self.myArray?

Yes, but no. Both point to the same object, the same area in memory. If you read myArray or self.myArray, they're identical in behavior minus the message send overhead for self.myArray.

However if you assign to myArray, the object will not be retained. It will only be retained if you assign to self.myArray.

Do I have to release self.myArray as well?

No.

You can also choose to either release or set the property to nil. As long as the property is @synthesize'd both examples do the same thing:

-(void) dealloc
{
    [super dealloc];
    [myArray release];
}

-(void) dealloc
{
    [super dealloc];
    self.myArray = nil;
}

See here for a discussion of the pros/cons to each approach.

From the question I think you're the developer who should really be using ARC. You'll have less to learn and fewer technical problems down the road. I can't understate how important using ARC is in these days, specifically if you don't have much ObjC experience. Read this how to enable ARC for cocos2d or just use Kobold2D to be able to work with an ARC-enabled cocos2d out of the box.

Community
  • 1
  • 1
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks for the answer. I have a question though, why is releasing either the property or field enough? I thought that since I've used retain twice I would have to release it twice? And I really should make the jump to ARC but this is bugging me hee. :) – Moo33 Oct 18 '12 at 03:42