Reputation: 1
I am sending NSStrings
many times, between classes, and i think i am loosing them.
lets say i have : classA ,classB, classC
classA
having instance of classB
(allocated) and he is calling function in classB,
the function in B:
-(NSMutableData*)setProtocolDataForString:(NSString*)data WithCommand:(int)comm
{ //here i can see and log the string 'data', but i am sending him to classC from here
[classCInstance send:data];
}
classA
call that function with : ..setProtocolDataForString:@"ran"];
As i wrote inside, i can see that classB get the string,but after he pass him to another classC ,i think it is being lost.
How exactly functions save/retain the value of NS
arguments they get ?
How would i do it right so this string can be passed between infinite classes and not freed?
**dont know if its important,but classA/B dont use ARC, but classC does
Upvotes: 0
Views: 65
Reputation: 4614
You have to retain the string because class A and B are not ARC enabled.
-(NSMutableData*)setProtocolDataForString:(NSString*)data WithCommand:(int)comm
{ //here I can see and log the string 'data', but I am sending him to classC from here
[data retain];
[classCInstance send:data];
}
Upvotes: 0
Reputation: 1021
Either allocate or retain NSString object before pass it to class method.
Upvotes: 0
Reputation: 2623
Class B
needs to have it's own copy of the string it is passing on to class C
. Since NSString
instances are considered to be value objects you do not simply retain a string, but create a copy instead.
NSString newString = [aString copy];
in this scenario aString
can safely be released since you already have a copy with its very own retain count which is not going to be released.
Don't forget to release / auto-release your copy. This is not automatically done for you. If you forget this, your application will leak memory.
The following code should work for you:
[classCInstance send:[[data copy] autorelease]];
Upvotes: 3