views:

85

answers:

5

Hi,

I use an object to get some values with it and return this values. The values which will be returned are still in this object. Heres the code:

    XMLErrorParser *xmlErrorParser = [XMLErrorParser alloc];
    [xmlErrorParser parseData: data];
    return xmlErrorParser.errors;

So how can i release the xmlErrorParser Object and return the values of it? Thanks.

+1  A: 

That's what autorelease is for (could it be that you forgot init?):

XMLErrorParser *xmlErrorParser = [[[XMLErrorParser alloc] init] autorelease];
[xmlErrorParser parseData: data];
return xmlErrorParser.errors;

Read the Memory Management Guide for Cocoa.


Depending on the purpose of your method, you might have to retain xmlErrorParser.errors as well.

Felix Kling
`xmlErrorParser` might already be autoreleased. I think Apple's own accessors do that.
Georg
@Georg: Why should it be already autoreleased? It is explicitly allocated and initialized. No object is autoreleased automatically if it is created this way.
Felix Kling
@Felix: I'm sorry, I meant `xmlErrorParser.errors`.
Georg
+3  A: 
XMLErrorParser *xmlErrorParser = [[XMLErrorParser alloc] init];
[xmlErrorParser parseData: data];
return [xmlErrorParser autorelease].errors;

or better

XMLErrorParser *xmlErrorParser = [[[XMLErrorParser alloc] init] autorelease];
[xmlErrorParser parseData: data];
return xmlErrorParser.errors;
Jason Coco
A: 

I suppose parseData is your initializer, In that case you can use the autorelease message to let the innermost autorelease pool know that you no longer need the object.

Example:

XMLErrorParser *xmlErrorParser = [XMLErrorParser alloc];
[[xmlErrorParser parseData: data] autorelease];
return xmlErrorParser.errors;

I advise you explicitly retain the errors property as well, otherwise you can lose track of it. Rule of thumb, release and autorelease calls together must match the number of retains in order to dealloc an object.

Denis 'Alpheus' Čahuk
init missing and autorelease on what?!
Eiko
I mentioned that I was assuming that parseData is his initializer, meaning that it returns an id type, more specifically the new object it initialized, making this code valid. Please read it through a couple of times next time before spamming/flagging.
Denis 'Alpheus' Čahuk
+5  A: 

Just return an auto-released version of the object errors holds.

Without giving us more details about what XMLErrorParser is, lets assume that errors holds some NSArray:

XMLErrorParser *xmlErrorParser = [[XMLErrorParser alloc] init];
[xmlErrorParser parseData: data];
NSArray *errors = [[xmlErrorParser.errors retain] autorelease];
[xmlErrorParser release];
return errors;

(Note that you were missing the initialization for the error parser object.)

Georg Fritzsche
+1 The cleanest solution here, because you retain/autorelease just the data you need.
Eiko
On the other hand, you're using 2 lines more + your example is considerably less legible than the others.
Georg
A: 

How about - autorelease?

swegi