简体   繁体   English

iPhone NSMutableArray发布问题

[英]iPhone NSMutableArray release problem

I have two controllers, SyncController and XMLController. 我有两个控制器,SyncController和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. SyncController将一些参数发送到XMLController,XMLController连接到API,并将结果包装为NSMutableArray中的对象,然后将数组返回给SyncController。

Some code: 一些代码:

SyncController.h SyncController.h

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

SyncController.m 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 XMLController.h

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

XMLController.m 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. 如果我不释放它,代码将正常运行(// DO SOME STUFF中的操作已执行),但显然会泄漏。 If I release it, wherever I do it (see the commented //[objects release]; in three places) the app crashes. 如果我释放它,无论我在哪里执行(请查看注释的/// [objects release];在三个地方),应用程序都会崩溃。

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 . 也许您要在- (void)parserDidEndDocument:(NSXMLParser *)parser重新分配对象,然后在- (void)dealloc重新分配对象。 Try to set the object to nil (≠ NULL) when you release it, so you know it won't get released again. 尝试在释放对象时将其设置为nil(≠NULL),这样就知道它不会再次释放。

- (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. 您正在将objects定义为保留属性,然后直接处理实例变量。 If you are using synthesize to generate the getters and setters, then let them do the memory management for you. 如果使用synthesize生成getter和setter,则让它们为您执行内存管理。

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. 仍然不起作用,但是我感觉到问题可能出在Object类中。 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]; 如果我评论[self.name版本]; 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. 但是随后,该应用程序在使用“对象名称”属性的其他地方泄漏了NSString。

It might be something very trivial that I miss. 我想念的东西可能很琐碎。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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