Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-16-2010, 07:20 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 7
Default 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?
Alec is offline   Reply With Quote
Old 03-16-2010, 12:30 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Talking

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.

Code:
//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:

Code:
//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:
Code:
-(void) dealloc{

     [datetime release];
     [myPicture release];

     [self dealloc];
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-17-2010, 07:38 AM   #3 (permalink)
Registered Member
 
headkaze's Avatar
 
Join Date: Feb 2010
Posts: 343
Default

Is there a way to list the variables that still have a reference/retain count when you exit your app?
headkaze is offline   Reply With Quote
Old 03-17-2010, 12:15 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by headkaze View Post
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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 03-20-2010, 02:09 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 7
Default

Quote:
Originally Posted by smasher View Post
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.

Code:
//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:

Code:
//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:
Code:
-(void) dealloc{

     [datetime release];
     [myPicture release];

     [self dealloc];
}
Ok, thank you. That help me a lot.
Alec is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 250
24 members and 226 guests
ADY, AragornSG, bookesp, chillyh, dacapo, Dani77, Davey555, Dominus, dre, glenn_sayers, HemiMG, JasonR, karlam963, LEARN2MAKE, M.A.S., marshusensei, mer10, nobre84, Oral B, prchn4christ, Raggou, Rudy, spiderguy84, themathminister
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:27 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0