user440096
user440096

Reputation:

App crashes when I try and release an object

I've kept the code as simple as I can. And included only what is really necessary for the app to function as it needs to.

But I'm still getting a crash that i cannot explain. There is no error message. It appeared when I added the line [parser release];

parser which is a XMLParser object crashes when I release, happens even if its a locally alloced and released or if i make it a member variable and release in the dealloc method.

Here is the code of the little class its used in.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class WebServiceAPI, RadioAppDelegate, XMLParser;
@interface WebServiceAPI : NSObject
{   
    XMLParser *parser;
    FootballRadioAppDelegate *appDelegate;
}
@end


#import "WebServiceAPI.h"
#import "XMLParser.h"

@implementation WebServiceAPI

-(void) getRadioStationList//:(id) aDelegate;
{
    NSURL *url = [[NSURL alloc] initWithString:@"http://bdoyle.kingpinhosting.com/Radios.xml"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [url release];
    parser = [[XMLParser alloc] initXMLParser];
    [xmlParser setDelegate:parser];
    [xmlParser parse];

    [xmlParser release];
    xmlParser = nil;    
}
- (void) connectionDidFinishLoading:(NSURLConnection*)connection
{
}   

- (void) connection:(NSURLConnection*) connection didFailWithError:(NSError*) error
{
}   

-(void) dealloc
{
    [parser release];
    parser = nil;
    [super dealloc];
}
@end

If I leave the code without a call to release of 'parser' then it works perfect. But obviously it's a memory leak.

Upvotes: 0

Views: 251

Answers (4)

HelmiB
HelmiB

Reputation: 12333

You can try to check whether have a allocate memory on an object by using

NSLog(@"parser allocation %d",[parser retainCount])

and try to empty the delegate

[parser setDelegate:nil]

hope it helps.

Upvotes: 0

bbum
bbum

Reputation: 162722

If parser is an instance variable, the commented out release in radioStationList would be wrong. That would not be a leak assuming you want the parser to survive until dealloc.

If XMLParser is following the standard delegate pattern, then it won't retain parser and, thus, that release would likely lead to an over-release or crash later.

There should be a backtrace if you are crashing. Or a crash log. Post it, please. Also, try running with Zombies enabled and see if you get more information.

Upvotes: 0

Catfish_Man
Catfish_Man

Reputation: 41821

It looks like you're releasing parser twice; once in dealloc and once in getRadioStationList.

Upvotes: 1

seppo0010
seppo0010

Reputation: 15869

You are releasing parser on the "getRadioStationList" method and not assigning it to nil. Then on the dealloc of the class you are releasing it again.

You should avoid releasing twice or assign to nil after releasing.

Upvotes: 1

Related Questions