Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Class in NSMutableArray

I have a class named:
Data
and it have variables:
NSString *datetime, UIImage *myPicture;
Class doesn't have any alloc or release methods for these variables.

In another class I put Data- class objects to the nsMutableArray *dataArray with addObject- method.

//Local variables
NSString *datetime;
UIImage *myPicture;
Data *myData = [[Data alloc] init];//Does nothing else than retain class

NsDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@”yyyy-MM-dd HH:MM:SS];
datetime = [formatter stringFromDate:date];

//The next is coming from the camera
myPicture = [info objectForKey:UIIMagePickerControllerOriginalImage];

myData.datetime = datetime;
myData.myPicture = myPicture;

[dataArray addObject: myData];

[myData.datetime release];
[myData.myPicture release];
[datetime release];
[myPicture release];
[formatter release];
[myData release];


The question is, do I need use retain with datetime and myPicture variables(like myData.myPicture = [myPicture retain];) when I put data to myData.datetime and myData.myPicture or can nsMutableArray addObject- method retain myData.datetime and myData.myPicture variables when it retain myData- class?
Post edited by Alec on

Replies

  • smashersmasher Posts: 3,858Registered Users
    Ask yourself at all times "who is retaining this object?" If no one is retaining an object it'll get destroyed. You're releasing too much - much too much. In this snippet the only thing you should release are formatter and myData. For now let's look at datetime instead.


    //stringFromDate returns autorelease string
    datetime = [formatter stringFromDate:date];

    myData.datetime = datetime;

    [dataArray addObject: myData];

    //release the string, destroying it - it'll now crash later
    //when the autorelease pool is drained or you try to access the object
    [myData.datetime release];

    //release the string again; will probably crash right now
    [datetime release];

    [myData release];


    *IF* the property datetime is declared as (nonatomic,retain) -- which is what you want for objects -- then this is the correct way to do what you're doing:


    //stringFromDate returns autorelease string
    datetime = [formatter stringFromDate:date];

    myData.datetime = datetime;

    [dataArray addObject: myData];

    //no need to release anything - datetime is in the autorelease pool,
    //and myData is the only thing retaining it. If you had alloc/init'ed
    //datetime (not gotten an autorelease string) then you would
    //release it here.

    //release mydata; it's being retained by the array,
    //so it doesn't get destroyed
    [myData release];


    You should write a dealloc method for your Data class so that it can release its properties when the object gets destroyed. It'll look something like this:

    -(void) dealloc{

    [datetime release];
    [myPicture release];

    [self dealloc];
    }
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • headkazeheadkaze Posts: 400Tutorial Authors, Registered Users
    Is there a way to list the variables that still have a reference/retain count when you exit your app?
  • smashersmasher Posts: 3,858Registered Users
    headkaze;183657 said:
    Is there a way to list the variables that still have a reference/retain count when you exit your app?
    Yes, the "leaks" instrument will show an list (updated every few seconds) of the objects that you've "leaked" - that is, any object that is still retained but which you have no pointers to. You can also drill down to see where that object was created; then you can look at your code to figure out what's wrong with its life cycle.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • AlecAlec Posts: 7New Users
    smasher;183397 said:
    Ask yourself at all times "who is retaining this object?" If no one is retaining an object it'll get destroyed. You're releasing too much - much too much. In this snippet the only thing you should release are formatter and myData. For now let's look at datetime instead.


    //stringFromDate returns autorelease string
    datetime = [formatter stringFromDate:date];

    myData.datetime = datetime;

    [dataArray addObject: myData];

    //release the string, destroying it - it'll now crash later
    //when the autorelease pool is drained or you try to access the object
    [myData.datetime release];

    //release the string again; will probably crash right now
    [datetime release];

    [myData release];


    *IF* the property datetime is declared as (nonatomic,retain) -- which is what you want for objects -- then this is the correct way to do what you're doing:


    //stringFromDate returns autorelease string
    datetime = [formatter stringFromDate:date];

    myData.datetime = datetime;

    [dataArray addObject: myData];

    //no need to release anything - datetime is in the autorelease pool,
    //and myData is the only thing retaining it. If you had alloc/init'ed
    //datetime (not gotten an autorelease string) then you would
    //release it here.

    //release mydata; it's being retained by the array,
    //so it doesn't get destroyed
    [myData release];


    You should write a dealloc method for your Data class so that it can release its properties when the object gets destroyed. It'll look something like this:

    -(void) dealloc{

    [datetime release];
    [myPicture release];

    [self dealloc];
    }
    Ok, thank you. That help me a lot.
Sign In or Register to comment.