开发者

sqlite query in iphone causes leak

I would like to know if calling a method in the following format is any reason for LEAKS?

[userLookupWS initWithUsername:[NSString stringWithUTF8String:(char *)sqlite3_column_text(select_system, 2)] andPassword:[NSString stringWithUTF8String:(char *)sqlite3_column_text(select_system, 3)] andURL:[NSString stringWithFormat:@"%@%@", [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_system, 0)], [NSString stringWithUTF8String:(char *)sqlite3_column_text(select_system, 1)]] andSSL:[NSString stringWithUTF8String:(char *)sqlite3_col开发者_如何学编程umn_text(select_system, 4)]];

What is the other way round to pass parameters to this method as I am getting 100% memory leak in this statement!

Regards, Accilies


Why are you sending initWithUsername: to an object stored in a variable? You should be passing the result of alloc there directly (userLookupWS = [[SomeClass alloc] initWithUsername:…]), and don't ever re-initialize an existing instance.

Aside from just being strange (it's initialized already! Why are you initializing it again?), nearly all init… methods are written for the assumption that they'll only be called once per instance, so sending an initWithWhatever: message to an already-initialized instance will leak everything that that instance owns.

There is no good way to fix that except to simply not do that in the first place. Don't send any init message to an already-initialized instance. The easiest way to avoid this is to only send an init message directly to the return value of alloc ([[SomeClass alloc] init…]).

And, of course, anything alloc returns, you have to release. The easiest way to ensure that happens is to autorelease the object immediately: [[[SomeClass alloc] init…] autorelease].


The initxxx method name suggest that an object is returned that is your responsibility to free, so yes if you don't release that object you may get a memory leak.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜