views:

31

answers:

2

Code Sample:

NSString *str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 1)];
Test *t=[[Test alloc] init];
t.str=[str copy]; // why use "copy" here?
[str release];
+1  A: 

It looks to be bad coding, for two reasons. Since str is being discarded, you might as well assign it, rather than a copy, to t.str. Second, and more importantly, setters should take care of retaining or copying data.

outis
+1  A: 

Should be:

NSString *str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 1)];
Test *t=[[Test alloc] init];
t.str=str; // No point copying to release original.
Ben S
The release is most likely correct, for the particular code block is probably done with `str`.
outis
@outis - while the `release` was correct in order to balance the `retain` implied by `copy`, the use of `copy` itself is unnecessary (as you accurately describe in your answer)
Dave DeLong
@Dave: You're right; I overlooked the fact that str is autoreleased.
outis