Help ! Get array from another class (Dao) and release it

Discussion in 'iOS Programming' started by cthesky, Oct 12, 2011.

  1. cthesky macrumors member

    Aug 21, 2011

    I have a function in a Dao class which return an Array. I get array from that function and used it in another class but when I release that array, the application crash when I run it.

    Here is my code:


    //Funtion to get data from database and return an NSMutableArray.
    -(NSMutableArray*) selectTypesArray
    NSMutableArray * types = [NSMutableArray alloc]init];

    // Some code here to get data from database.

    return types;



    Dao dao = [[Dao alloc]init];

    NSMutableArray * temp = [dao selectTypesArray];

    NSMutableArray * types = temp;


    [temp release];
    [dao release];


    -(void)displayTypes: (NSMutableArray*)types
    NSLog(@"Types count %i", [types count]);

    When I run the program, it crash with the error message : "-[__NSArrayM count]: message sent to deallocated". I think that this happens is because I have released "temp" and "types" reference to an deallocated object ... ...But I don't know how to write this code in a correct way. Anyone can tell me the correct way to write this code?

    Any comments and suggestions are welcome and appreciated. Thanks a lot. :)
  2. jiminaus macrumors 65816


    Dec 16, 2010
    You need to (re)view the Memory Management Programming Guide to learn how to use retain, release and autorelease correctly so that your objects are neither deallocated prematurely nor leaked because they aren't deallocated at all.

Share This Page