![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
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? |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
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;
} |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
&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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
Wow. Thanks for the information guys. I came back hoping someone answered and already three people did.
|
|
|
|
|
|
#6 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
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) |
|
|
|
|
|
|
#8 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#9 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
![]() |
|
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|