Scofield Tran
Scofield Tran

Reputation: 840

object c - release an object which is owner from 2 other objects

from researching memory management on objective C, I have a question about reference count:

I have an object name obj_number. I have alloc it

    obj_number = [[NSNumber alloc] init];

and then in another method (named A)of this class, I set

    obj_number = [dataset objectAtIndex:0];

go out this method A dataset will be autorelease. I checked this after go out this method obj_number also can't be access, maybe it also be deallocate.

I see if an object just be under one ownership, so if parent deallocate it also be deallocate, but in my case I suppose that obj_number under 2 ownership (retain count may equals to 2), so why running out method A it be released?

Upvotes: 2

Views: 86

Answers (3)

trojanfoe
trojanfoe

Reputation: 122391

You need to provide a setter method for obj_number, which you can do by providing a @property in the .h file and a @sythesize in the .m file. The setter method will ensure that the old value is released and the new value is retained.

MyClass.h:

@interface MyClass : NSObject
{
    NSNumber *_obj_number;    // Use a different name for the ivar and property!
    ...
}

@property (retain, nonatomic, readwrite) NSNumber *obj_number;

...

@end

MyClass.m:

@implementation MyObject
@synthesize obj_number = _obj_number;

...

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

And when assigning to obj_number, you must relinquish ownership:

- (void)someMethod
{
    // We own myNumber, so release
    NSNumber *myNumber = [[NSNumber alloc] initWithUnsigned:12];
    self.obj_number = myNumber;
    [myNumber release];

    // However this is easier:
    self.obj_number = [NSNumber numberWithUnsigned:12];
}

- (void)someOtherMethod
{
    // We don't own [dataset objectAtIndex:0] so no need to release
    self.obj_number = [dataset objectAtIndex:0];
}

Upvotes: 0

Pierre
Pierre

Reputation: 2019

You shouldn't think in retain count. The important thing is the ownership.

obj_number = [[NSNumber alloc] init];

Creates a new NSNumber instance that you own. This means that you have to release it when it is not longer needed.

obj_number = [dataset objectAtIndex:0];

This gives you an object that you don't own. You you don't have to release it. If you want the ownership you must send it a retain message and must release it later.

Another point for your code. If you write something like that you have a memory leak (if you don't use ARC):

//obj_number = [[NSNumber alloc] init]; // returns nil, so no leak
obj_number = [[NSNumber alloc] initWithInt:1];
some code // No [obj_number release] or [obj_number autorelease] here
obj_number = [dataset objectAtIndex:0];

The last line causes the leak. After that line obj_number points to the object in dataset at index 0 and no longer to the NSNumber you allocated at the beginning.

EDIT: Like trojanfoe said, in most cases it is better to use properties as they do the memory management for you.

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

EDIT: Are you using non-ARC ?

In method, you might have like this:

-(void)A{
    ....
    obj_number = [dataset objectAtIndex:0];
    [dataset autorelease];
}

In the above you are changing the pointer of obj_number to point to [dataset objectAtIndex:0].

As per your under 2 ownership (retain count may equals to 2)

I hope your intension would be to copy or retain this value, right?

Then you can use obj_number = [[dataset objectAtIndex:0] copy];//or retain only then its retain count will increase to 2.

Upvotes: 1

Related Questions