Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 12th, 2006, 1:46 PM   #1
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Destructors and how the OS handles program memory

In c++, you have to use destructors. I'm having some problems with this as mine never seem to get called.

Anyway, my main class, `Game`, is used until the return statement of the main() return statement. If my operating system is just going to delete all my program memory, I figure I may as well let the operating system destruct it for me.

So I have three questions:
1) Does the operating system free all memory allocated by a program on exit?
2) Would this method work on all major operating systems?
Harakim is offline   Reply With Quote
Old May 12th, 2006, 3:12 PM   #2
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
A Windows and Linux system will only deallocate static memory. If you create by doing this
variable something = new something

then the operating system will not delete it. When an OS deallocates a class though, it will call your destructors for you. But if there is nothing in the dtor to delete it, it will not get deleted.

As for number 2, I know this is how it works for Windows and Linux, presumably it would work the same way on other OS's. It likely has more to do with the compiler really.
Twilight is offline   Reply With Quote
Old May 12th, 2006, 4:03 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
The only time an object' destructor would not be called is when it's constructor did not fully construct the object. I imagine you'd know about it if that was happening. If you want to dynamically allocate an object without needing to explicitly call delete, then consider using std::auto_ptr.

ex:
#include <memory>

class MyClass
{
};

typedef std::auto_ptr<MyClass> MyClass_ptr;

int main()
{
	MyClass_ptr myClass( new MyClass );
	return 0;
}
Just make sure you understand auto_ptr's first. Don't use them in standard containers ect.
Cache is offline   Reply With Quote
Old May 12th, 2006, 4:58 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
&c ...
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 12th, 2006, 6:57 PM   #5
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Wow. Thanks for the information guys. I came back hoping someone answered and already three people did.
Harakim is offline   Reply With Quote
Old May 12th, 2006, 7:36 PM   #6
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by Twilight
A Windows and Linux system will only deallocate static memory. If you create by doing this
variable something = new something

then the operating system will not delete it. When an OS deallocates a class though, it will call your destructors for you. But if there is nothing in the dtor to delete it, it will not get deleted.

As for number 2, I know this is how it works for Windows and Linux, presumably it would work the same way on other OS's. It likely has more to do with the compiler really.
Under Windows and Linux, any memory you allocate using new will be freed when the program exits. The destructors for objects allocated in this way will not be called without a call to delete.
That said, you should call delete for each new in your program, just in case you ever want to include the code in some other program, or put a loop around it.
The Dark is offline   Reply With Quote
Old May 12th, 2006, 7:41 PM   #7
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by Harakim
In c++, you have to use destructors. I'm having some problems with this as mine never seem to get called.

Anyway, my main class, `Game`, is used until the return statement of the main() return statement. If my operating system is just going to delete all my program memory, I figure I may as well let the operating system destruct it for me.

So I have three questions:
1) Does the operating system free all memory allocated by a program on exit?
2) Would this method work on all major operating systems?
Before you start using auto_ptr as the end-all fix for this, you might want to look into why you think the destructors aren't getting called.

I think you'll find that they are being called, it's just that the method you're using to determine if it reached the destructor is invalid. Are you using std::cout or some form of Message Box? The reason I ask, is because the order that objects are destroyed isn't defined, so I'd imagine for you to "test" if the destructor is called try doing the following:

int main()
{
    {
        Game g;
    } // this guarantees that g's destructor will be called before your program exits
    return 0;
}

I know it looks silly, but by forcing the block-scope for your Game instance, it will go out of scope and get destructed before the rest of your program does.

(If I'm way off base, someone please correct me. It's been awhile since I've used C++ but I remember running into a similar issue when I was learning it)
Jason Isom is offline   Reply With Quote
Old May 12th, 2006, 7:52 PM   #8
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by Jason Isom
the order that objects are destroyed isn't defined
Maybe not in C#. In C++ objects are destructed in reverse order to there construction. FILO, if I remeber correctly.
Cache is offline   Reply With Quote
Old May 12th, 2006, 8:20 PM   #9
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by Cache
Maybe not in C#. In C++ objects are destructed in reverse order to there construction. FILO, if I remeber correctly.
Well, that makes my entire post invalid so please ignore.
Jason Isom is offline   Reply With Quote
Old May 13th, 2006, 2:15 AM   #10
Harakim
Hobbyist Programmer
 
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3 Harakim is on a distinguished road
Quote:
Originally Posted by Jason Isom
int main()
{
    {
        Game g;
    } // this guarantees that g's destructor will be called before your program exits
    return 0;
}

I know it looks silly, but by forcing the block-scope for your Game instance, it will go out of scope and get destructed before the rest of your program does.

(If I'm way off base, someone please correct me. It's been awhile since I've used C++ but I remember running into a similar issue when I was learning it)
I actually have read this method several places and I am glad that you posted in case I hadn't known it.
As for my constructor not being called, it's one of those things you don't like to tell anyone, but:
I was editting source in one directory and compiling in another. No matter how I changed the program, it was still using the old code. Heh, live and learn.
Harakim is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting 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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC