开发者

Memory management issue with NSString

开发者 https://www.devze.com 2023-01-27 21:09 出处:网络
I have NSMutableArray* cityData that I fill with custom LocationDetail objects. cityData is created in viewDidLoad and released in dealloc.

I have NSMutableArray* cityData that I fill with custom LocationDetail objects. cityData is created in viewDidLoad and released in dealloc. Somewhere in the code, based on user actio开发者_如何转开发ns, I populate LocationDetail and add it to cityData array:

LocationDetail* d = [[LocationDetail alloc] init];
    d.city = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 1)];
    d.tz = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 3)];
    d.country = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 2)];
    d._id = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 0)];

    [cityData addObject:d];
    [d release];

When I am finished with the view controller and remove it, Leaks utility says I have a leak in the code above in NSCFString in all 4 lines with [NSString stringWithFormat] above.

I tried removing the sqlite3 stuff and simplified the call to something like

d._id = [NSString stringWithFormat:@"%s", "a string"]

with the same result. However, if I replace the NSString stringWithFormat like this:

d._id = @"a string";

the leak goes away. I wonder why there is a leak if I use the stringWithFormat, but not if I use @"something". Is there anything obvious that I'm doing wrong?

Thanks!


Properties are not automatically released for you, you need to do that yourself in

- (void)dealloc

See The Objective-C Programming Language: Declared Properties for an example.


Edit:

It seems that the example was moved into the Advanced Memory Management Programming Guide.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号