Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-15-2011, 07:15 AM   #1 (permalink)
Flash Developer
 
Join Date: Mar 2011
Location: Norway
Posts: 77
thh022 is on a distinguished road
Default Help with these memory leaks

I got three memory leaks I can't figure out. These leaks I found by using Instruments > Leaks.

Leak 1: This leak I have no idea what is or what causes it.

Code:
#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil); // LEAK
    [pool release];
    return retVal;
}
Leak 2: Here I make some objects (NSMutableDictionary) and put them in the elements array. I release the array elements in dealloc.

Code:
NSMutableArray *elements;
NSMutableDictionary *item;
...
[elements addObject:[item copy]]; // LEAK
[item release];
Leak 3: Here the first and third lines comes up as a leak. In the dealloc-function I release the parser and set's it delegate = nil.

Code:
parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.rabaldus.no/apps/..."]]; // LEAK
[parser setDelegate:self];
[parser parse]; // LEAK
Maybe I need to submit more code, but hopes this is enough for a start to figure out the leaks.
thh022 is offline   Reply With Quote
Old 07-15-2011, 07:20 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

Leak1: the leak is not here, that is the autorelease pool
Leak2: you don't need to use copy
Leak3: you should release parse...since that you use alloc\init
__________________
dany_dev is offline   Reply With Quote
Old 07-15-2011, 07:23 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

You never keep a pointer to the object returned when you copy item, so it leaks.

When you copy it, you get a +1 retain count on the new object, you then add it to the array so it gets retained again. You're now at +2.

You need to do something like:
Code:
id copy = [item copy];
[elements addObject:copy];
[copy release];
harrytheshark is offline   Reply With Quote
Old 07-15-2011, 07:54 AM   #4 (permalink)
Flash Developer
 
Join Date: Mar 2011
Location: Norway
Posts: 77
thh022 is on a distinguished road
Default

Leak 1: I am not sure what this is. I can't find any class named main.m.

Leak 2: Code changed and noe more leaks:

Code:
[elements addObject:item copy];
Leak 3: Now I release the parser, but still the to lines is leaking:

parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.rabaldus.no/apps/..."]]; // LEAK
[parser setDelegate:self];
[parser parse]; // LEAK
[parser release];
thh022 is offline   Reply With Quote
Old 07-15-2011, 01:11 PM   #5 (permalink)
Flash Developer
 
Join Date: Mar 2011
Location: Norway
Posts: 77
thh022 is on a distinguished road
Default

Leak 3:

I found a solution on the web:

Original code:

Code:
parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.rabaldus.no/apps/***.php"]];
Updated code with no leaks:

Code:
NSData *dataXml = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.rabaldus.no/apps/***.php"]];
parser = [[NSXMLParser alloc] initWithData:dataXml];
[dataXml release];
As a newbie I canīt see the difference between the two ways, but the leak is gone.

Maybe someone can explain it?

Last edited by thh022; 07-16-2011 at 12:24 PM.
thh022 is offline   Reply With Quote
Old 07-15-2011, 01:16 PM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 194
mavrik5150 is on a distinguished road
Default

For the first leak trying running the Zombie Instrument instead of the Leak. It should show you what item is being either being released twice, or not released and therefore causing an issue when another item is trying to allocate that same memory space. I had this problem two and the Zombie instrument showed me the exact item that was causing the Zombie and I was able to fix it from there.
mavrik5150 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 356
8 members and 348 guests
apatsufas, Kirkout, leostc, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0