tags:

views:

19

answers:

2

This question follows on from a previous question. However stackoverflow presents me from commenting - like it sometimes prevents me from setting a correct answer on my own question.

Anyway I have some weirdness happening in my application. I have a property:

@property (nonatomic, retain) NSMutableArray *hotelList; //I also synthesize it

This is how I am setting the property:

- (void)populateHotelList
{
    SearchWebServiceController *searchWS = [[SearchWebServiceController alloc]init]; 

    //If I remove this retain the App crashes
    hotelList = [[searchWS getHotelsByRegionCode:@"12345" AndByKid:@"12345"] retain]; 

    [searchWS release];
}

However if I remove the retain my application crashes. But according to Apple documentation I should NOT need to retain it?!

This is the method's implementation:

- (NSMutableArray *)getHotelsByRegionCode:(NSString *)regionCode 
                                 AndByKid:(NSString *)Kid
{
    NSMutableArray *result = [[NSMutableArray alloc]init];

    ...

    return [result autorelease];
}

Can anyone please help!

A: 

You must retain it because your method returns autorelease object that will be destroyed in the next event loop.

spbfox
But its 2 different classes. So the called class is autoreleasing it and the calling class is retaining it? Can you explain if you have a chance?
TheLearner
Also the property is has a retain attribute
TheLearner
It is fine that those are different classes because the object they work with is the same, so they will affect the same retain counter. However the fact that the property is a retain one makes my posting incorrect. That also means that the problem is not in the code you posted. Can you tell where exactly the code fails without retain?
spbfox
I can't figure that out does this help: 2010-08-26 11:34:55.556 Renovatio[13062:207] *** -[CFXPreferencesSearchListSource count]: unrecognized selector sent to instance 0x60469602010-08-26 11:34:55.556 Renovatio[13062:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[CFXPreferencesSearchListSource count]: unrecognized selector sent to instance 0x6046960'2010-08-26 11:34:55.557 Renovatio[13062:207] Stack: (
TheLearner
Yes, it is retain problem (see similar post in here: <http://stackoverflow.com/questions/2927947/object-not-getting-released-in-iphone>). It sounds weird, but I have a feeling that hotelList that you have assigned in the function is NOT the same hotelList declared as a property. To eliminate that, could you please put "self." qualifier before it? The other suspision is that you have release call for hotelList somewhere else. Do you? When does the exception get thrown? Right after you exit the function or at some other place later?
spbfox
Adding the self qualifier worked!? Thanks mate - very strange!
TheLearner
A: 

Added the self qualifier and it worked.

self.hotelList = [searchWS getHotelsByRegionCode:@"12345" AndByKid:@"12345"];

Thanks to spbfox!

TheLearner