简体   繁体   中英

iPhone NSMutableArray release problem

I have two controllers, SyncController and XMLController. SyncController sends some parameters to XMLController, which connects to an API and wraps the result as objects within an NSMutableArray, and returns the array back to the SyncController.

Some code:

SyncController.h

-(void)urlHandler:(NSArray *)urlHandler listObjectsFinishedLoading:(NSMutableArray *)resultData;

SyncController.m

- (void)urlHandler:(NSArray *)urlHandler listObjectsFinishedLoading:(NSMutableArray *)resultData;
{
  NSMutableArray *receivedObjects = [[NSMutableArray alloc] init];
  [receivedObjects addObjectsFromArray:resultData];
  for (Object *o in receivedObjects) {
     //DO SOME STUFF
  }
  [receivedObjects release];
}

XMLController.h

@interface XMLController : NSObject {
    NSMutableArray *objects;
}
@property (nonatomic, retain) NSMutableArray *objects;

XMLController.m

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
  objects = [[NSMutableArray alloc] init];

  if ([delegate respondsToSelector:@selector(urlHandler:listObjectsFinishedLoading:)]) {
    [delegate urlHandler:self listObjectsFinishedLoading:objects];
  }

  //[objects release];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
  // Initialize an Object
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
  // Put some properties unto Object
  // Ad Object to the objects array
  // release Object
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
   //[objects release];
}

- (void)dealloc {    
    //[objects release];
    [super dealloc];
}

My question is, how do I properly release the objects array? If I don't release it, the code works properly (the actions from //DO SOME STUFF are executed) but it obviously leaks. If I release it, wherever I do it (see the commented //[objects release]; in three places) the app crashes.

Any suggestions? Thanks.

Try to

if ([delegate respondsToSelector:@selector(urlHandler:listObjectsFinishedLoading:)]) {
  [delegate urlHandler:self listObjectsFinishedLoading:[objects autorelease]];
}

Maybe you're deallocating the Object at - (void)parserDidEndDocument:(NSXMLParser *)parser and again in - (void)dealloc . Try to set the object to nil (≠ NULL) when you release it, so you know it won't get released again.

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
   [objects release], objects = nil;
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSMutableArray *temp=[[NSMutableArray alloc] init];
self.objects=temp;
[temp release];


  if ([delegate respondsToSelector:@selector(urlHandler:listObjectsFinishedLoading:)]) {
    [delegate urlHandler:self listObjectsFinishedLoading:objects];
  }

  //[objects release];
}


and then

- (void)dealloc {    
    //[objects release];
    [self.objects release];
    [super dealloc];
}

use in this way it definitely works.

You are defining objects as a retained property, and then addressing the instance variable directly. If you are using synthesize to generate the getters and setters, then let them do the memory management for you.

self.objects = [[[NSMutableArray alloc] init] autorelease];

and

self.objects = nil;

rather than manually doing the releases.

thanks for your help. Still doesn't work, but I get the feeling that teh problem might be in the Object class. Releasing the array calls release on every object right?

Object.h

@interface Object : NSObject {
    NSString *name;
}
@property (nonatomic, retain) NSString *name;

Object.m

-(void) dealloc{

  [self.name release]; 
  [super dealloc];
}

If I comment the [self.name release]; line, then the array in question CAN be released, it doesn't crash, and with no leaks. But then the app leaks NSStrings in other places where the Object name property is used.

It might be something very trivial that I miss.

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM