![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: deconstructor, help, how to, programming, tutorial |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| No, the lack of a destructor doesn't cause memory leaks. Forgetting to delete memory causes memory leaks. A destructor is sometimes useful if you have new'ed memory somewhere and you wish for your class to clean up after itself. In this case, writing a destructor can help, in most cases, you'll not need to write one. The syntax is similar to a constructor (Except that it must take 0 arguments and cannot be overloaded), but with the bit complement operator in front Code: class myclass
{
~myclass()
{
//delete etc.
}
}; |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (10-07-2007) | ||
| |||
| Essentially you just want to make it Available for the Garbage Collector to come get it - like unreference it from items. If it has no references and isn't being used, then the GC can come "collect" that memory. You can also force it (GC.Collect) but generally it's enough to make it available for pickup and it will be dealt with accordingly ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| The Following User Says Thank You to ccoonen For This Useful Post: | ||
HelloWorld (10-07-2007) | ||
| |||
| Quote:
to clean up memory allocated with new, use the delete keyword. If you forget to delete, this is when you get memory leaks. Code: //Allocating & deallocating space for a single object int* foo = new int; delete foo; Code: //Allocating & deallocating space for an array of 20 ints int* bar = new int[20]; delete [] bar; |
| |||
| Sorry, I thought when we were talking about VC++ which has the Finalize method to destruct ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
![]() |
| Thread Tools | |
| Display Modes | |
| |