2

I have a class XMLParser. It works fine as long as I don't remove the commenting of the line that adds the nodes to the NSMutableArray:

[nodes addObject:self.currentNode];

If I do that, the application crashes - without leaving any trace in the debug log. How can this be?

XMLParser.m:

#import "XMLParser.h"
#import "MyViewController.h"
#import "Node.h"


@implementation XMLParser
@synthesize currentNode, currentProperty, currentAddress, nodes;


- (NSMutableArray *)parseNodeData:(NSString *)url {
    NSURL *urlObj = [[NSURL alloc] initWithString:url];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:urlObj];

    self.nodes = [[NSMutableArray alloc] init];

    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
    NSLog(@"Proceeded from parser.");

    NSLog(@"Returning %@ rows", [nodes count]);
    //[parser release];
    return self.nodes;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.currentProperty) {
        [currentProperty appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    if (qName) {
        elementName = qName;
    }

    NSLog(@"<%@>", elementName);

    if(self.currentNode)
    {
        if ([elementName isEqualToString:@"id"] || [elementName isEqualToString:@"name"])
        {
            self.currentProperty = [NSMutableString string];
        }
    } else {
        // We are outside of everything, so we need a
        // Check for deeper nested node
        if ([elementName isEqualToString:@"node"]) {
            self.currentNode = [[Node alloc] init];
            NSLog(@"Initialized new node...");
        }
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if(self.currentNode)
    {
        if ([elementName isEqualToString:@"id"]) {
            NSLog(@"Tried setting node id: %@", self.currentProperty);
            self.currentNode.nodeId = self.currentProperty;

        } else if ([elementName isEqualToString:@"name"]) {
            NSLog(@"Tried setting node name: %@", self.currentProperty);
            self.currentNode.name = self.currentProperty;
        } else if ([elementName isEqualToString:@"node"]) {
            NSLog(@"Adding node to array");
            //[nodes addObject:self.currentNode];
            self.currentNode = nil;
        }
    }

    NSLog(@"</%@>", elementName);

    self.currentProperty = nil;
}

- (void) dealloc {
    [currentNode release];
    [currentAddress release];
    [currentProperty release];
    [super dealloc];
}


@end

XMLParser.h:

#import <UIKit/UIKit.h>
#import "Address.h"
#import "Node.h"

@class XMLAppDelegate;

@interface XMLParser : NSObject {
    NSMutableString *currentProperty;
    Address *currentAddress;
    Node *currentNode;
    NSMutableArray *nodes;

}

@property (nonatomic, retain) NSMutableString *currentProperty;
@property (nonatomic, retain) Address *currentAddress;
@property (nonatomic, retain) Node *currentNode;
@property (nonatomic, retain) NSMutableArray *nodes;

- (NSMutableArray *)parseNodeData:(NSString *)data;

@end

EDIT: Output from the debug log

[Session started at 2009-05-26 23:31:26 +0200.]
2009-05-26 23:31:28.352 HelloWorld[6914:20b] <response>
2009-05-26 23:31:28.353 HelloWorld[6914:20b] <nodesearchresult>
2009-05-26 23:31:28.354 HelloWorld[6914:20b] <resultsreturned>
2009-05-26 23:31:28.354 HelloWorld[6914:20b] </resultsreturned>
2009-05-26 23:31:28.354 HelloWorld[6914:20b] <resultsfound>
2009-05-26 23:31:28.354 HelloWorld[6914:20b] </resultsfound>
2009-05-26 23:31:28.355 HelloWorld[6914:20b] <nodes>
2009-05-26 23:31:28.355 HelloWorld[6914:20b] <node>
2009-05-26 23:31:28.356 HelloWorld[6914:20b] Initialized new node...
2009-05-26 23:31:28.356 HelloWorld[6914:20b] <id>
2009-05-26 23:31:28.356 HelloWorld[6914:20b] Tried setting node id: 614
2009-05-26 23:31:28.356 HelloWorld[6914:20b] </id>
2009-05-26 23:31:28.357 HelloWorld[6914:20b] <name>
2009-05-26 23:31:28.357 HelloWorld[6914:20b] Tried setting node name: Roberts coffee
2009-05-26 23:31:28.357 HelloWorld[6914:20b] </name>
2009-05-26 23:31:28.358 HelloWorld[6914:20b] <address>
2009-05-26 23:31:28.358 HelloWorld[6914:20b] <street>
2009-05-26 23:31:28.358 HelloWorld[6914:20b] </street>
2009-05-26 23:31:28.358 HelloWorld[6914:20b] <zipcode>
2009-05-26 23:31:28.359 HelloWorld[6914:20b] </zipcode>
2009-05-26 23:31:28.359 HelloWorld[6914:20b] </address>
2009-05-26 23:31:28.359 HelloWorld[6914:20b] <position>
2009-05-26 23:31:28.359 HelloWorld[6914:20b] <latitude>
2009-05-26 23:31:28.360 HelloWorld[6914:20b] </latitude>
2009-05-26 23:31:28.360 HelloWorld[6914:20b] <longitude>
2009-05-26 23:31:28.360 HelloWorld[6914:20b] </longitude>
2009-05-26 23:31:28.361 HelloWorld[6914:20b] </position>
2009-05-26 23:31:28.361 HelloWorld[6914:20b] <phone>
2009-05-26 23:31:28.361 HelloWorld[6914:20b] </phone>
2009-05-26 23:31:28.363 HelloWorld[6914:20b] <nodetypes>
2009-05-26 23:31:28.363 HelloWorld[6914:20b] <nodetype>
2009-05-26 23:31:28.363 HelloWorld[6914:20b] </nodetype>
2009-05-26 23:31:28.364 HelloWorld[6914:20b] </nodetypes>
2009-05-26 23:31:28.364 HelloWorld[6914:20b] <rating>
2009-05-26 23:31:28.364 HelloWorld[6914:20b] <score>
2009-05-26 23:31:28.365 HelloWorld[6914:20b] </score>
2009-05-26 23:31:28.365 HelloWorld[6914:20b] <votes>
2009-05-26 23:31:28.366 HelloWorld[6914:20b] </votes>
2009-05-26 23:31:28.366 HelloWorld[6914:20b] </rating>
2009-05-26 23:31:28.366 HelloWorld[6914:20b] <teaser>
2009-05-26 23:31:28.367 HelloWorld[6914:20b] </teaser>
2009-05-26 23:31:28.367 HelloWorld[6914:20b] Adding node to array
2009-05-26 23:31:28.367 HelloWorld[6914:20b] </node>
2009-05-26 23:31:28.367 HelloWorld[6914:20b] <node>
2009-05-26 23:31:28.368 HelloWorld[6914:20b] Initialized new node...
2009-05-26 23:31:28.368 HelloWorld[6914:20b] <id>
2009-05-26 23:31:28.368 HelloWorld[6914:20b] Tried setting node id: 326
2009-05-26 23:31:28.368 HelloWorld[6914:20b] </id>
2009-05-26 23:31:28.369 HelloWorld[6914:20b] <name>
2009-05-26 23:31:28.369 HelloWorld[6914:20b] Tried setting node name: Sort kaffe & vinyl
2009-05-26 23:31:28.369 HelloWorld[6914:20b] </name>
2009-05-26 23:31:28.369 HelloWorld[6914:20b] <address>
2009-05-26 23:31:28.370 HelloWorld[6914:20b] <street>
2009-05-26 23:31:28.370 HelloWorld[6914:20b] </street>
2009-05-26 23:31:28.371 HelloWorld[6914:20b] <zipcode>
2009-05-26 23:31:28.371 HelloWorld[6914:20b] </zipcode>
2009-05-26 23:31:28.372 HelloWorld[6914:20b] </address>
2009-05-26 23:31:28.372 HelloWorld[6914:20b] <position>
2009-05-26 23:31:28.372 HelloWorld[6914:20b] <latitude>
2009-05-26 23:31:28.373 HelloWorld[6914:20b] </latitude>
2009-05-26 23:31:28.373 HelloWorld[6914:20b] <longitude>
2009-05-26 23:31:28.373 HelloWorld[6914:20b] </longitude>
2009-05-26 23:31:28.374 HelloWorld[6914:20b] </position>
2009-05-26 23:31:28.374 HelloWorld[6914:20b] <phone>
2009-05-26 23:31:28.375 HelloWorld[6914:20b] </phone>
2009-05-26 23:31:28.375 HelloWorld[6914:20b] <nodetypes>
2009-05-26 23:31:28.375 HelloWorld[6914:20b] <nodetype>
2009-05-26 23:31:28.375 HelloWorld[6914:20b] </nodetype>
2009-05-26 23:31:28.376 HelloWorld[6914:20b] <nodetype>
2009-05-26 23:31:28.376 HelloWorld[6914:20b] </nodetype>
2009-05-26 23:31:28.376 HelloWorld[6914:20b] </nodetypes>
2009-05-26 23:31:28.376 HelloWorld[6914:20b] <rating>
2009-05-26 23:31:28.377 HelloWorld[6914:20b] <score>
2009-05-26 23:31:28.377 HelloWorld[6914:20b] </score>
2009-05-26 23:31:28.377 HelloWorld[6914:20b] <votes>
2009-05-26 23:31:28.377 HelloWorld[6914:20b] </votes>
2009-05-26 23:31:28.378 HelloWorld[6914:20b] </rating>
2009-05-26 23:31:28.378 HelloWorld[6914:20b] <teaser>
2009-05-26 23:31:28.379 HelloWorld[6914:20b] </teaser>
2009-05-26 23:31:28.379 HelloWorld[6914:20b] Adding node to array
2009-05-26 23:31:28.379 HelloWorld[6914:20b] </node>
2009-05-26 23:31:28.380 HelloWorld[6914:20b] </nodes>
2009-05-26 23:31:28.380 HelloWorld[6914:20b] </nodesearchresult>
2009-05-26 23:31:28.381 HelloWorld[6914:20b] </response>
2009-05-26 23:31:28.381 HelloWorld[6914:20b] Proceeded from parser.

[Session started at 2009-05-26 23:31:28 +0200.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".warning: Unable to read symbols for "/System/Library/Frameworks/UIKit.framework/UIKit" (file not found).
warning: Unable to read symbols from "UIKit" (not yet mapped into memory).
warning: Unable to read symbols for "/System/Library/Frameworks/CoreLocation.framework/Versions/A/CoreLocation" (file not found).
warning: Unable to read symbols from "CoreLocation" (not yet mapped into memory).
warning: Unable to read symbols for "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics" (file not found).
warning: Unable to read symbols from "CoreGraphics" (not yet mapped into memory).
Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/philipdahlstrm/Library/Application Support/iPhone Simulator/User/Applications/6BD1CC99-F133-4B97-A041-1D210851B6C6/HelloWorld.app/HelloWorld', process 6914.
(gdb) 
4
  • No trace in the debug log? So you don't even see "Adding node to array" printed in your console, nor do you see a stack trace when the program crashes? Try cleaning your project and choosing "Debug" from the "Run" menu. Commented May 26, 2009 at 20:56
  • Also, I'd recommend NSLogging self.nodes and self.currentNode before that commented line that causes the crash to make sure they are valid object pointers. Commented May 26, 2009 at 20:57
  • 1
    Not sure how this could have anything to do with the problem, but you the code is leaking Node objects. When you create it with alloc and assign it to the property currentNode its retain count is incremented (you are on 2). Later, when you add it to the array, it's incremented once again, to a count of 3, and you never release the object, but set a reference as nil. Commented May 26, 2009 at 21:16
  • Your code is also leaking an NSMutableArray (again, you assign a retained object to a retain property). Have you tried setting a symbolic breakpoint on objc_exception_throw()? I think you're going to have to show us Node.h. Commented May 26, 2009 at 22:49

1 Answer 1

3

Your bug is in the final NSLog:

NSLog(@"Returning %@ rows", [nodes count]);

%@ takes a pointer to an object. [nodes count] returns an unsigned integer, which you then dereference as a pointer. It's ok if nodes is empty, because then -count return 0, which is nil, which is safe to dereference this way. You should be using %d here rather than %@.

The debugger stack should have shown you the exact place this was happening. I found it by looking at the last line of the log and seeing what happened immediately after that.

You are leaking a ton of memory here, btw. If you use one of the Three Magic Words in an assignment to a retain-accessor (like self.nodes =...), you need to autorelease.

self.currentNode = [[Node alloc] init];

Should be:

self.currentNode = [[[Node alloc] init] autorelease];

And you should always use accessors for ivars. Never access nodes directly the way you are (except in their accessors and in dealloc).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.