开发者

Can't see why there is a memory leak here

I have the following block giving me problems in the performance tool: Particularly it is saying STObject is leaking. I am not sure why?

for (NSDictionary *message in messages)
{
    STObject *mySTObject = [[STObject alloc] init];

    mySTObject.stID = [message valueForKey:@"id"];

    [items addOb开发者_高级运维ject:mySTObject];
    [mySTObject release]; mySTObject = nil;

} 
[receivedData release]; receivedData=nil;
[conn release]; conn=nil;

UPDATE:

items is @property(nonatomic, retain) will this cause the retain count to be +2?


If you add something to a NSArray or NSDictionary its retained, your mySTObject is retained, meaning it still exists when you do - release and then set it to nil. Remove the object from the storage where it is retained and your "leak" is gone.


Do you have some variables/properties stored in STObject? If you do, you will need to release them in "-(void) dealloc" method of STObject. Otherwise, although STObject is released, the variables own by the STObject will not get released.

An example of dealloc method will be:

- (void)dealloc {
    [stID release];
    [myVar2 release];
    [myVar3 release];
    [super dealloc];
}

Also make sure that you call [super dealloc] at the end of the method.


If you are on 10.6, Xcode has "build and analyze" which I find is a very good tool for debugging memory leaks. Documentation is available here


You are setting mySTObject to nil after releasing it...

        [mySTObject release]; mySTObject = nil;

just remove mySTObject = nil; I think that should be it..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜