+4  A: 

You don't actually use the objects you initialise at the top of the function:

flashCardText = [[NSString alloc] init];
flashCardAnswer=[[NSString alloc] init];

as you replace those objects with others later on:

self.flashCardText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(CompiledStatement,0)];
self.flashCardAnswer= [NSString stringWithUTF8String:(char *)sqlite3_column_text(CompiledStatement,1)];

So those would seem to be the objects that are leaking.

Graham Lee
i have removed those two lines but still showing leak..i am adding a picture from instruments
Rahul Vyas
i am using flashcardtext to show text on a label...but i am using flashcardanswer in same class at 3-4 times...but showing text in another class...if i remove self from flashcardanswer app. will crash
Rahul Vyas
They would not leak as the property would (presumably) release the (pointless) initial values of flashCardText and flashCardAnswer.
Peter N Lewis
+1  A: 

[[NSString alloc] init]; is a useless phrase. Removing the two first lines would get rid of the leak.

EDIT: Note, too, that the two strings you pull from the database will vanish as soon as the autoreleasepool is drained, unless they are retained.

Redit: Concerning the second method; I cannot see any obvious leaks. NSCFStrings are created a lot, and often stick around. That doesn't mean they actually are leaks. From what I can see, everything in that method is either autoreleased or persistent.

Williham Totland
Good point about the autorelease, I hadn't picked up on that. Goes to show it's possible to focus too much on the question that was asked :-)
Graham Lee
so should i create a property in header file like this @property (nonatomic, retain) NSString *flashCardText; or like this@property (nonatomic, copy) NSString *flashCardText;
Rahul Vyas
`@property(nonatomic,copy)` is generally considered best for strings, insofar as I recall.
Williham Totland
Yes, copy is best, because you could otherwise retain a mutable string that someone else is editing, in which case your object no longer has control over its state.
Graham Lee
so could this line cause application crash @property (nonatomic, retain) NSString *flashCardText;
Rahul Vyas
Using retain To NSString will it cause any trouble?
Rahul Vyas
Using "retain" for an NSString wont cause problems - the problem is when you have "retain" for a property, and someone sets the property to an NSMutableString. Since it's retained, it can then be changed without your objects knowledge. Hence the recomendation to use "copy" instead of "retain" for NSString properties. But the memory manamgent issue is equivalent either way, since boht "copy" and "retain" take an ownership of the NSString
Peter N Lewis
The two strings pulled from the database are stored in properties, so as long as the properties are set to "copy" they will not vanish when the autorelease pool is drained.
Peter N Lewis
@Peter: Yeah, I didn't notice they were properties. (I secretly hate most of the new features of Obj-C 2.0 :P )
Williham Totland
+1  A: 

[[NSString alloc] init] is completely pointless. It will return exactly equivalent of @"", except wiht more work. Its unlikely to leak, since the system will almost certainly return you a fixed constant empty string, releasing the [NSString alloc] in the init routine. But it is pointless and useless and bad code.

Other than that, your code looks OK. The second method might be considered to "leak" optionsList, simply because it creates it and it is never released, but its only created once so it should be fine.

Try running your program and doing the leak detection, then breaking in the debugger and using

po 0x4b2720 (replace with the address of the leaked objects)

to see what string is actually leaking.

Remember that Leaks can give false positives, especially if anything is cached.

Peter N Lewis
+1 for the debugging methods.
Abizern
this doesn't works po 0x4b2720 (replace with the address of the leaked objects) gdb prints nothing...
Rahul Vyas
A: 

-1/2

@"" is equivalent to [[[NSString alloc] init] autorelease]; [[NSString alloc] init] will leak.

Remember the rule: init/retain should be balanced by autorelease/release.

Andrew Hooke