PDA

View Full Version : releasing Objects [iPhone]




sujithkrishnan
Jul 4, 2008, 02:05 AM
Hi all.


In my app i am populating an object (myCatalogObject)through unarchiving....

At the end,when i call release to that object, its calling the dealloc. (its OK)

the dealloc is like below



(void*) dealloc
{
[myMutableDictionary release];
[myMutableArray release];
}


But its giving "EXEC_BAD_ACCESS" for the first line in dealloc method....

I populate the myCatalogObject like below



Catalog* myCatalogObject = [NSUnarchiver unarchive.......];


The unarchiving is doing perfectly, but i want to relase the memory occuped by object before quitting application...


the Catalog class contains a MutableDictionary and MutableArray.....


Thanks...



HiRez
Jul 4, 2008, 02:43 AM
First of all, why are you using that C-style format for an Objective-C dealloc method? For example, the dash marking it as an instance method. Looks odd to me. Also, you should be calling super in your dealloc method. I would have something more like this:

- (void)dealloc {
[myMutableDictionary release];
[myMutableArray release];
[super dealloc];
}

Did you hold onto the myMutableDictionary and myMutableArray objects when you unarchived them by sending them retain messages? If not, they're probably invalid when you try to release them, leading to the EXEC_BAD_ACCESS error.

sujithkrishnan
Jul 4, 2008, 03:30 AM
First of all, why are you using that C-style format for an Objective-C dealloc method? For example, the dash marking it as an instance method. Looks odd to me. Also, you should be calling super in your dealloc method. I would have something more like this:

- (void)dealloc {
[myMutableDictionary release];
[myMutableArray release];
[super dealloc];
}

Did you hold onto the myMutableDictionary and myMutableArray objects when you unarchived them by sending them retain messages? If not, they're probably invalid when you try to release them, leading to the EXEC_BAD_ACCESS error.

I didnt get how to retain the myMutableDictionary and myMutableArray, explicitly....

I am just unarchiving the entire object...

Did u mean ... after archiving, i want to call retain to both of them?

my dealloc is like below only (what u mentioned)

- (void)dealloc {
[myMutableDictionary release];
[myMutableArray release];
[super dealloc];
}

HiRez
Jul 4, 2008, 04:28 AM
I am just unarchiving the entire object...

Did u mean ... after archiving, i want to call retain to both of them?Oh, I see, you are archiving the entire object graph at once. Well, you still need to retain that myCatalogObject after you unarchive it, which means you need to release it at some point. Are you releasing that object as well in the dealloc: method of whatever class it belongs to (like your application controller maybe)? In other words, do you have the line [myCatalogObject release]; anywhere? Is that dealloc: method you listed the dealloc: for the Catalog class, or something else?

sujithkrishnan
Jul 4, 2008, 04:53 AM
Oh, I see, you are archiving the entire object graph at once. Well, you still need to retain that myCatalogObject after you unarchive it, which means you need to release it at some point. Are you releasing that object as well in the dealloc: method of whatever class it belongs to (like your application controller maybe)? In other words, do you have the line [myCatalogObject release]; anywhere? Is that dealloc: method you listed the dealloc: for the Catalog class, or something else?

Yeah..
i am calling the "[myCatalogObject release]" in my viewController, in turn that release calling the "dealloc" of Catalog (class of myCatalogObject) ...
(dellaoc is calling as retainCount is 0, isnt it...thats what i want...)
but in the "dealoc" of Catalog i release the dictionary and array, i got error when i call these two lines....

Do i want to retain myCatalogObject or dictionary & array ????

But if i call retain for myCatalogObject, retainCount of that while application terminating is "1", instead it is zero now (which is fine and apt...)