Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > dealloc not being called on my control

dealloc not being called on my control
Thread Tools
poulh
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 9, 2005, 12:41 PM
 
ok... i made a simple app to verify that I'm not doing something wrong in the project I'm working on... here is the simplified example.

I made a new cocoa app (NOT document based). I put a button in the window. I made a class off of NSObject called MyControl and added an action called pushMe. I hooked it to the button, instantiated/created the files and added NSLog(@"push me"); to the func. Compiled, ran it and I see the print-out when I push the button. So far so good.


Now... I added an init and an awakeFromNib to the control's .m file and put NSLogs is those.. again everything worked... I see the print outs.

then I added

- (void) dealloc
{
NSLog(@"inside dealloc");
[super dealloc];
}

I never see this text, why???
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 9, 2005, 01:39 PM
 
Should the control ever be deallocated?
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 9, 2005, 01:42 PM
 
Originally Posted by Chuckit
Should the control ever be deallocated?
I don't know? how is my data cleaned up in my control?

I guess I'm looking for a place to release my control's member variables when I'm quitting. Where should I do this?
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 9, 2005, 02:06 PM
 
Originally Posted by poulh
I don't know? how is my data cleaned up in my control?

I guess I'm looking for a place to release my control's member variables when I'm quitting. Where should I do this?
From -dealloc.
However, -dealloc probably won't be called when you quit, because it's more efficient to blow away the entire app's address space and all the memory it allocated in one fell swoop than it is to go through and individually free all the objects and other bits of memory that it allocated over its lifetime.
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 9, 2005, 04:08 PM
 
Originally Posted by Angus_D
it's more efficient to blow away the entire app's address space and all the memory it allocated in one fell swoop than it is to go through and individually free
Is this how its done?

If that is the case do I just have to hook up an action to the Quit Menu if I want to do something when the program quits?
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 9, 2005, 04:34 PM
 
You could hook up Quit to a custom action, but the more Cocoa way for simple tasks would be to either use the applicationShouldTerminate: in the app's delegate (if you want to intervene) or subscribe to the NSApplicationWillTerminateNotification if you just want to know when it's quitting.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 9, 2005, 04:39 PM
 
Originally Posted by Chuckit
You could hook up Quit to a custom action, but the more Cocoa way for simple tasks would be to either use the applicationShouldTerminate: in the app's delegate (if you want to intervene) or subscribe to the NSApplicationWillTerminateNotification if you just want to know when it's quitting.
As well as being the "more Cocoa way", it will also work if your application is being terminated for reasons other than the Quit menu item being selected (e.g. if you invoke +[NSApp terminate:] yourself, or if you close the last window in an app which is set up to terminate when the last window is closed, etc).
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
May 9, 2005, 04:49 PM
 
Your dealloc method isn't being called because the default setting for windows in Interface Builder is to keep them in memory after they've been closed so that they can be reopened again. To change, select the window in IB and hit cmd-1 and check Release When Closed. When the window is closed, it will be sent a "release" message. Unless you've explicitly retained the window, this will cause its retainCount to drop to zero and the window's dealloc method will be called. This results in all of the window's views being released, which will cause your control to be released and (unless you've explicitly retained it), its dealloc method will also be called.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 9, 2005, 05:07 PM
 
Originally Posted by smeger
Your dealloc method isn't being called because the default setting for windows in Interface Builder is to keep them in memory after they've been closed so that they can be reopened again. To change, select the window in IB and hit cmd-1 and check Release When Closed. When the window is closed, it will be sent a "release" message. Unless you've explicitly retained the window, this will cause its retainCount to drop to zero and the window's dealloc method will be called. This results in all of the window's views being released, which will cause your control to be released and (unless you've explicitly retained it), its dealloc method will also be called.
Thanks, this might be my problem. But why isn't the window's release method being called when I click the Quit Menu?
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
May 9, 2005, 10:58 PM
 
Originally Posted by poulh
But why isn't the window's release method being called when I click the Quit Menu?
What Angus_D said. If you need to clean up stuff when your app quits, you need to create an application delegate (generally a simple object that's a subclass of NSObject) and implement the applicationWillTerminate: method.

Not much to it.

Or you can register for the NSApplicationWillTerminateNotification with NSNotificationCenter. That'll do it, too.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 10, 2005, 02:05 PM
 
Originally Posted by smeger
you need to create an application delegate (generally a simple object that's a subclass of NSObject) and implement the applicationWillTerminate: method.

Ok, I did this and it worked! Thanks to all. I still don't see why dealloc isn't called though.


So if I have a member pointer in my controller class I should release it in applicationWillTerminate (this is where i put it)?
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 10, 2005, 02:48 PM
 
Originally Posted by poulh
I still don't see why dealloc isn't called though.
Why would it be called? Is the window being deallocated during the lifetime of your program?

Originally Posted by poulh
So if I have a member pointer in my controller class I should release it in applicationWillTerminate (this is where i put it)?
As Angus_D said, you do not need to release objects when your application is terminating. It's a waste of time.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 10, 2005, 02:51 PM
 
Originally Posted by Chuckit
Why would it be called? Is the window being deallocated during the lifetime of your program?
My thinking is that when a program quits it has to free all its memory... therefore all the objects have to be destructed. I'm assuming dealloc is the same as C++'s destructor.

That is why I'm surprised my controller isn't calling it.. how is its memory being freed?
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 10, 2005, 02:52 PM
 
Originally Posted by poulh
That is why I'm surprised my controller isn't calling it.. how is its memory being freed?
When an application dies, its resources are freed by the kernel.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
poulh  (op)
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status: Offline
Reply With Quote
May 10, 2005, 02:58 PM
 
ahhh so no function is being called first (except the delagates)... I'm coming from C++ land so that helps!
12" Aluminum Powerbook
1.5Ghz G4 | 512Mb Ram | GeForce FX Go5200
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:03 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,