views:

1586

answers:

1

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)
+3  A: 

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).

Rob Napier