Nitin
Nitin

Reputation: 7471

Strange Memory leak

I have been working with iPhone application in which i am using sqlite database. I have checked memory leak in iPad,iPhone 4 and there is no memory leaks. But same code show memory leak in iPhone-3GS. Please take a look at below scree-shot and i think there is no leak than after instruments showing me leaks. Please do advice me.

Thanks in Advance.

enter image description here

Upvotes: 0

Views: 144

Answers (3)

user529758
user529758

Reputation:

The problem is this:

NSArray *Query = [[NSArray alloc] init];
// later:
Query = [database executeQuery:str];

Thusly, you allocate an NSArray, then reuse its pointer, so you loose the reference to the firstly allocated object. You don't need to do

NSArray *Query = [[NSArray alloc] init];

as the executeQuery: method returns an initialized array.

The same holds to Query1 as well (you're also allocating it erronously).

(By the way, are you using my SQLHelper library? If so, you must not release the array returned by the query; it's autoreleased and will crash upon overreleasing)

Upvotes: 3

X Slash
X Slash

Reputation: 4131

That's because you're allocating NSArray for Query and Query1 but never use them, you immediately change the pointer to something else which is your [xxxx executeQuery:xxx]. Change them to these and it should be fine.

NSArray* Query = [database executeQuery:str];
NSArray* Query1 = [database executeQuery:str1];

Upvotes: 1

JustSid
JustSid

Reputation: 25318

Its actually not that strange, see how you allocate memory for both Query and Query1 via [[NSArray alloc] init. But then, you overwrite the pointer of them by calling [database executeQuery:], so the allocated memory is leaked.

It looks like you don't really need the array allocation anyway, so you can simply remove that line. However, that the [Query release] and [Query1 release] doesn't crash the app (or at least at some later point), indicates that [database executeQuery:] return retained memory and thus delegates its ownership to the caller, which is discouraged by the memory guidelines. You should return an autoreleased object from executeQuery: and because you never use the returned value, you can simply reduce the method down to:

[self databaseOpen];
[database executeQuery:@"Delete from ActivityList"];
[database executeQuery:@"Select* from ActivityList"];

Upvotes: 4

Related Questions