views:

415

answers:

4

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 addObject: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?

A: 

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

        [mySTObject release]; mySTObject = nil;

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

lukya
This is not an issue here.
quixoto
That would be a bug in the leaks tool. Are you sure that would fix it?
JeremyP
I think he is used to GC objective-c, but that would crash it in this case.
Antwan van Houdt
+4  A: 

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.

tmin
+1 I think this is likely the answer although leaks should point to the STObject code instead. However, leaks isn't perfect.
TechZen
Yes, I would also look at the property declaration for stID, which could also be retaining... maybe.
livingtech
+5  A: 

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.

Antwan van Houdt
Releasing the array will have the same effect, as the array will release everything it had in it when it gets deallocated.
Peter Hosey
Ofcourse, but the code snippet is too small for me to see what he is doing, just trying to help.
Antwan van Houdt
Where do I release the array? items is in a helper class that gets passed to the delegate. I am releasing it in dealloc().
Sheehan Alam
Release when you do not need it anymore, and yes if its a class variable you should do this in dealloc (which is called when the retain count reaches 0 )- (void) dealloc{ [someClassArray release]; someClassArray = nil; [super dealloc];}NSObject's memory management is pretty easy, it allocates the object with malloc ( with some typedefs and shit ) and returns the pointer then when you retain or release it you just either do +1 or -1 to a instance var of NSObject counting the retain count, when it reaches 0 dealloc is called and the object gets freed ( free(void *pointer); )
Antwan van Houdt
To be clear, retaining does the +1 and releasing does the -1—you don't keep the retain count yourself, as NSObject does that for you in response to retain and release messages. Sheehan Alam: If you are releasing the array in `dealloc`, then the problem is elsewhere. Use Instruments to find out what retained the object and didn't release it.
Peter Hosey
Peter, Instruments is telling me the leak is in this block of code, even though I am releasing the array in the dealloc.
Sheehan Alam
Alternatively, you can put a trap on -retain message of your object (if you override this method) and see what object is retaining it if anything fails. And just check if this object is really being dealloced.
krzyspmac
How do you put a trap?
Sheehan Alam
A: 

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

alexy13