Accilies
Accilies

Reputation: 139

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_column_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

Upvotes: 0

Views: 279

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96323

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].

Upvotes: 1

AndersK
AndersK

Reputation: 36082

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.

Upvotes: 0

Related Questions