Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / November 2007



Tip: Looking for answers? Try searching our database.

Why does this leak memory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lorenzo Thurman - 19 Nov 2007 18:49 GMT
I have a subclass of NSXMLDocument that I use as an XMLParser. While
running my program through MallocDebug, I see that I am leaking large
blocks of memory in one of my methods. I'm at a loss to understand why.
Here is the function in question:

-(NSArray*)itemsForElement:(NSString*)el{
    NSError* err = nil;
    NSString* searchString = [NSString stringWithFormat:@".//%@", el];
    NSArray* nodes = [self nodesForXPath:searchString error:&err];
    int i = 0;
    if([nodes count] == 0)
        return nil;
   
    NSMutableArray* values = [[NSMutableArray alloc] init];
   
    for(i; i < [nodes count]; i++){
        [values addObject:[[[nodes objectAtIndex:i] objectValue]
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]]];
    }
   
    return [values autorelease];

}

The local variables I use are autoreleased and the mutable array is
autoreleased on return, correct? I have a couple of other programs that
when run MallocDebug, don't show any leaks. I bring that up because, I
think I'm fairly well versed on Cocoa memory management, but there must
be a hole in my understanding somewhere. I suspect its in the addObject
call, but I think that looks OK. I've included a portion of the
MallocDebug dump file in case that helps.
TIA

STACK
Address /096b6710/size/00000010
0x0006a4f0  / 0x6a4f0 / libMallocDebug.A.dylib
0x90c585d0  / _internal_class_createInstanceFromZone / libobjc.A.dylib
0x94316b88  / +[NSObject allocWithZone:] / CoreFoundation
0x92a43190  / -[NSXMLNode objectsForXQuery:constants:error:] / Foundation
0x92a43024  / -[NSXMLNode nodesForXPath:error:] / Foundation
0x00005ea8  / -[WeatherParser itemsForElement:] / Weather Vane
0x00008250  / -[WeatherVane forecastData] / Weather Vane
0x00007ee8  / -[WeatherVane parseData] / Weather Vane
0x0000767c  / -[WeatherVane parseAndUpdateData] / Weather Vane
0x00003150  / -[WeatherVane(WVURLConnection)
connectionDidFinishLoading:] / Weather Vane
0x929ff590  / _NSURLConnectionDidFinishLoading / Foundation
0x963f9218  / sendDidFinishLoadingCallback / CFNetwork
0x963f608c  / _CFURLConnectionSendCallbacks / CFNetwork
0x963f5914  / muxerSourcePerform / CFNetwork
0x942a239c  / CFRunLoopRunSpecific / CoreFoundation
0x93d39ab8  / RunCurrentEventLoopInMode / HIToolbox
0x93d398dc  / ReceiveNextEventCommon / HIToolbox
0x93d3971c  / BlockUntilNextEventMatchingListInMode / HIToolbox
0x90eac7a4  / _DPSNextEvent / AppKit
0x90eac1f4  / -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] / AppKit
0x90ea5eb0  / -[NSApplication run] / AppKit
0x90e76930  / NSApplicationMain / AppKit
0x00004f68  / main / Weather Vane
0x00002c7c  / _start / Weather Vane
0x00002958  / start / Weather Vane
ENDSTACK
David Phillip Oster - 20 Nov 2007 04:01 GMT
> I have a subclass of NSXMLDocument that I use as an XMLParser. While
> running my program through MallocDebug, I see that I am leaking large
[quoted text clipped - 60 lines]
> 0x00002958  / start / Weather Vane
> ENDSTACK

The stack trace, and the code, make it look like your line:
NSArray* nodes = [self nodesForXPath:searchString error:&err];
is the culprit. I'd give

- [WeatherParser nodesForXPath: error: ]
a good hard look.

Also, the ObjectAlloc tool, on the XCode menu Debug > Launch With
Performance Tool >
will tell you the history of a leaked object, where in your program it
was alloc'ed, released, retained, and autoreleased.
Lorenzo Thurman - 20 Nov 2007 17:18 GMT
>> I have a subclass of NSXMLDocument that I use as an XMLParser. While
>> running my program through MallocDebug, I see that I am leaking large
[quoted text clipped - 72 lines]
> will tell you the history of a leaked object, where in your program it
> was alloc'ed, released, retained, and autoreleased.
I don't override nodesForXPath, if that's what you mean. I use the
default instance method for the class. It should return objects which
are released when the container is released. Since the NSArray(nodes) is
not alloc'd, it should be released sometime after the
method(itemsForElement) returns. I look at ObjectAlloc and see what that
tells me.
Thanks
Lorenzo Thurman - 27 Nov 2007 17:13 GMT
>>> I have a subclass of NSXMLDocument that I use as an XMLParser. While
>>> running my program through MallocDebug, I see that I am leaking large
[quoted text clipped - 79 lines]
> tells me.
> Thanks
So, after testing my subclass with a subset of my project, neither Leaks
nor MallocDebug, reports a leak. So the problem must lie elsewhere in my
code. Any have any ideas how to troubleshoot this?
TIA
David Phillip Oster - 28 Nov 2007 06:28 GMT
...
> >> Also, the ObjectAlloc tool, on the XCode menu Debug > Launch With
> >> Performance Tool > will tell you the history of a leaked object, where
> >> in your program it was alloc'ed, released, retained, and autoreleased.

> So, after testing my subclass with a subset of my project, neither Leaks
> nor MallocDebug, reports a leak. So the problem must lie elsewhere in my
> code. Any have any ideas how to troubleshoot this?

Um, use the ObjectAlloc tool?
Lorenzo Thurman - 28 Nov 2007 22:04 GMT
> ...
>>>> Also, the ObjectAlloc tool, on the XCode menu Debug > Launch With
[quoted text clipped - 6 lines]
>
> Um, use the ObjectAlloc tool?
I did use ObjectAlloc and was not able to come to any conclusions. My
test program is subset of my main program. I'm going to build from
there, testing periodically to see how I may have introduced the leaks.
Shouldn't take very long as the program is not very big to begin with.
Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.