iphone - Dealloc of custom object -
i have class in project :
@interface videoitem : nsobject <nscoding> { nsstring *name; nsstring *artist; int seconds; } @property (nonatomic, retain) nsstring *name; @property (nonatomic, retain) nsstring *imgurl; @property (nonatomic, retain) nsstring *artist; @end
and how create object:
videoitem *item = [[videoitem alloc] init]; item.name = name; item.imgurl = imglink; item.artist = artist;
and dealloc:
- (void)dealloc{ [name release]; [imgurl release]; [artist release]; [super dealloc]; }
and want know if dealoc
ok non-arc
? did need else because nsstring
property?
edit
and if videoitem object create with:
videoitem *item = [[videoitem alloc] init]; item.name = [nsstring alloc]initwithformat@"%@",name]; item.imgurl = [nsstring alloc]initwithformat@"%@",imglink]; item.artist = [nsstring alloc]initwithformat@"%@",artist];
did in case dealloc still ok? or need change something?
everything looks ok, releasing @properties
of object. point them nil, make sure, if 1 of properties called, nilled , not have garbage value, so:
- (void)dealloc{ [name release], name = nil; [imgurl release], imgurl = nil; [artist release], artist = nil; [super dealloc]; }
another thing, no related, cleaner, if create own init, can pass properties values, when create object, so:
-initwithname:(nsstring *)name withimgurl:(nsstring *)imgurl withartist:(nsstring *)artist;
your edit:
item.name = [nsstring alloc]initwithformat@"%@",name]; item.imgurl = [nsstring alloc]initwithformat@"%@",imglink]; item.artist = [nsstring alloc]initwithformat@"%@",artist];
only based on this, create leak, should careful. fix this:
item.name = [[nsstring alloc]initwithformat@"%@",name] autorelease]; item.imgurl = [[nsstring alloc]initwithformat@"%@",imglink] autorelease]; item.artist = [[nsstring alloc]initwithformat@"%@",artist] autorelease];
Comments
Post a Comment