|
|
dealloc not being called on my control
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Should the control ever be deallocated?
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
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).
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
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'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Jun 2000
Location: New York, NY
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|