Hi,
perhaps is this kind of a trivial question, but I am a bit confused about the way to create an object-class and to use memory-management in a proper way. The reason for my question is that I have seen many different ways (in tutorials on the web/ in books etc) to do this. If I have an object, say a car and I want to use this object in some of my viewControllers to show alot of different cars, is this the right way to do it:
My Questions:
What about retaining the carTitle in this way? Are there risks with it? Is it correct to release it in the dealloc method?
What about the alloc/init of the carObject? And the release?
Is it ok to use @synthesize here? No need for manually programming setter/getter methods? Is the variable not globla in a " not so good" way by doing this?
I really want to understand this in order to create non-leaking apps and most of all apps not crashing all the time because of "over-releasing" the objects.
Thanks in advance!
MACLoop
perhaps is this kind of a trivial question, but I am a bit confused about the way to create an object-class and to use memory-management in a proper way. The reason for my question is that I have seen many different ways (in tutorials on the web/ in books etc) to do this. If I have an object, say a car and I want to use this object in some of my viewControllers to show alot of different cars, is this the right way to do it:
Code:
[COLOR="Red"]in car.h:[/COLOR]
NSString *carTitle;
@property(nonatomic, retain) NSString *carTitle;
[COLOR="Red"]in car.m[/COLOR]
@synthesize carTitle;
- (void)dealloc{
[carTitle release];
[super dealloc];
}
[COLOR="Red"]in the viewcontroller.m[/COLOR]
[COLOR="SeaGreen"]//create a car object[/COLOR]
Car *theCar = [[Car alloc]init];
theCar.carTitle = @"My test car";
[COLOR="SeaGreen"]//save all the objects in an array[/COLOR]
[carArray addObject:theCar];
[theCar release];
[COLOR="SeaGreen"]//later on - show it in for ex. a modal view when the user clicks on a table cell[/COLOR]
modalCarView = [[CarModalViewController alloc] initWithNibName:@"CarModalViewController" bundle:nil];
modalCarView.title = [carArray objectAtIndex:indexPath].title;
[self.navigationController pushViewController:modalCarView animated:YES];
[modalCarView release];
My Questions:
What about retaining the carTitle in this way? Are there risks with it? Is it correct to release it in the dealloc method?
What about the alloc/init of the carObject? And the release?
Is it ok to use @synthesize here? No need for manually programming setter/getter methods? Is the variable not globla in a " not so good" way by doing this?
I really want to understand this in order to create non-leaking apps and most of all apps not crashing all the time because of "over-releasing" the objects.
Thanks in advance!
MACLoop