objective c - Confused about alloc and release -
i'm little confused objective-c , allocating/releasing objects.
if this:
nsstring *mystring; if([somestring isequaltostring: @"test1"]){ mystring = @"got 1"; }else{ mystring = @"got 2"; }
do have release mystring
after that?
and same self-defined objects:
myownobject *someobject = [somearray objectatindex: 1]; mybutton.label1.text = someobject.name;
do have release someobject
?
the reason why i'm asking memory-leaks in method , can't find is. i'm trying figure out whether alloc/release stuff correctly. leak occurs on nsplaceholderstring (i guess that's somewhere hidden in nib-file).
also - if have object, allocate it, use of properties, release of every property on dealloc - cause memory leaks?
sorry - hope questions make @ least some sense :)
thanks help!
listen me. this rule matters.
if use method "copy", "alloc", "new", or "retain" in name
you own object , must later release or autorelease it.
if don't:
don't!
but don't expect object stick around outside of scope, because you don't own it.
it's simple.
myclass *foo = [[myclass alloc] init]; [array addobject:foo]; [foo release];
did use "copy", "retain", "new", or "alloc"? yes. release it.
myclass *someobject = [somearray objectatindex:0];
did use "copy", "retain", "new", or "alloc"? no. don't release it.
but
if have instance variable need access in other methods:
ivar = [[somearray objectatindex:0] retain];
then you're guaranteed stick around because own it.
(another way handle @property (retain)
properties, because can self.ivar = someobject
, it'll retain you.)
but remember release them in -dealloc
!
Comments
Post a Comment