Iphone delegate memory leak problem

cocoacocoa-touchiphoneiphone-sdk-3.0

I need your help, i'm stacked on this project.


When i run my applicaion into Instruments, →← marked line giving memoryleak. When the fist run everything is ok but second time →← marked line giving memory leak, i didn't understand why ?

I tried a lot of things, i used NSAutoReleasePool as well but result same.

Can someone help me pls ?

SBJSON *json = [[SBJSON alloc] init];
error = nil;
→ NSDictionary *results = [json objectWithString:response error:&error]; ←
[json release];
[response release];

NSArray *responsedNetworks = [results objectForKey:@"networks"];
NSMutableArray *serverResponse = [[NSMutableArray alloc] initWithArray:responsedNetworks];

Edit:
I uploaded my example application to this address : [removed][1] now, i'm sure that problem source isn't delegate, but i don't know where is the problem, I'm sure that Instruments shows wrong place for memory leak. I tried a lot of things can someone show me my mistake ?

Thank you

Edit 2:
Guys Unfortunately, i tried Clang static analyser,as i expected it gave a couple of warning into the some line of codes, I fixed these warnings.

Now, Instruments giving leak error, but Clang says there is not error into project.

Finally, My problem wasn't gone.

Please download lastest example application and you can see with your eyes Here is the
EXAMPLE Project source.

How to see these leaks :
Open project and start to debug on the simulator (version doesn't matter). Start debug, you will see info button top of the corner on the opened first window. Press and open it, after that close info window and open it again, at this point you should see Instrument's leak messages. But Clang doesn't give anything i mean everything is allright on Clang side.

I'm really confused ? what should i do ?

Best Answer

Instruments does not indicate the line that causes the leak. It indicates the line that allocated memory that later was leaked. Just from the code given (which is obviously not the entire code), I'd suspect that you're leaking serverResponse, which includes memory allocated in the line you've marked. Don't just look at the line that Instruments gives you. What class is leaking? That will give as much hint as the line.

Since you're starting to have trouble with memory management, a suggestion: when you release something, always set it to nil. This will save you a lot of headache and crashes later. In this case I'm talking about json and response:

[json release];
json = nil;

If you have Snow Leopard and Xcode 3.2, run the Analyzer (Cmd-Shift-A). It will find most common memory problems automatically.

Related Topic