ios - iPhone, passing object to method(s), memory management -
i know there lot of questions on topic already, not clear me yet. so, still wondering is, if call method , pass object, have retain object inside method use there. , if retain it, release it. lets make bit of more complex example. explain whats wrong this:
nsstring *mystr = [[nsstring alloc]initwithstring:@"hello"]; mystr = [self modstring2:[self modstring1:mystr]]; [mystr release]; //these methods... -(nsmutablestring*)modstring1:(nsstring*)str{ return [[[str stringbyappendingstring:@" mr. memory"] mutablecopy] autorelease]; } -(nsmutablestring*)modstring2:(nsstring*)str{ return [[[str stringbyappendingstring:@" how this?"] mutablecopy] autorelease]; }
this confusing me. lets assume create object inside method:
[self modstring:[self createstring]]; -(nsstring*)createstring{ nsstring *string = [nsstring stringwithstring:@"hello"]; return string; } -(nsmutablestring*)modstring:(nsstring *)str{ [str retain]; nsmutablestring *mut = [nsmutablestring stringwithstring:str]; return mut; }
would correct? thing: if copy string array string like:
nsstring *str = [nsstring alloc[ initwithstring:[[arr objectatindex:0]copy]]];
does retain whole array, or whats happening here? mean have release array? dont it. there practical resources apart apple`s? want understand this...
a method not own object getting passed argument?! right? , have retain object in method, if object object returned method (which called before) autorelease via: return [object autorelease] , therefore created within method, called @ first.
and one:
for example if following:
request = [[nsmutableurlrequest alloc] initwithurl:url];
can release url after this, or still have stick arround request valid?
if need inside method, don't need retain it.
if need store in instance variable use later, retain it, , release in dealloc method, or when assign new object instance variable.
examples concerning comment
if modify value of string (nsmutablestring), don't have worry. need release in method created string. may have problems if return pointer string, , if assign new string previous one. in such case, can't access original pointer anymore, , have memory leak can't release anymore
example 1
{ nsarray * arr = [ [ nsarray alloc ] initwithobject: @"foo", @"bar", nil ]; [ self somemethod: arr ]; [ arr release ]; } - ( void )somemethod: nsarray * arr { arr = [ nsarray emptyarray ]; }
this ok, no memory leak, if assign array in somemethod
, because pointer local method, , won't affect original pointer.
example 2
{ nsarray * arr = [ [ nsarray alloc ] initwithobject: @"foo", @"bar", nil ]; [ self somemethod: &arr ]; } - ( void )somemethod: nsarray ** arr { *( arr ) = [ nsarray emptyarray ]; }
here, have memory leak, modify original pointer. note used **, meaning have pointer pointer.
example 3
{ nsarray * arr = [ [ nsarray alloc ] initwithobject: @"foo", @"bar", nil ]; arr = [ self somemethod: arr ]; } - ( nsarray * )somemethod: nsarray * arr { return [ nsarray emptyarray ]; }
memory leak, we've redefined pointer arr
array. has been allocated, can't release since pointer points variable.
Comments
Post a Comment