Freeing memory

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Trimmy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Aug 05, 2010 1:16 am
Current Project: A Game
Favorite Gaming Platforms: PS3, PC ( Mac OS )
Programming Language of Choice: C++
Location: New Zealand
Contact:

Freeing memory

Post by Trimmy »

Hey, is it best to free dynamic memory in a class's destructor or in a member function?
Example:

Code: Select all

SomeClass::~SomeClass( void ) {
delete a;
delete [] b;
}
// or
void SomeClass::cleanUp( void ) {
delete a;
delete [] b;
}
Does it not matter ( obviously other than the fact the member function will have to be called )? Does it depend on the situation? Should the cleanUp function be called the destructor? Thanks for any help in advance and I hope this isn't an absolute noob question.
My Programming Projects:http://www.youtube.com/user/TrimmyS
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: Freeing memory

Post by X Abstract X »

Assuming that the dynamically allocated memory needs to remain allocated for the lifetime of the object, it should be freed in the destructor, that's what destructors are for.
User avatar
Trimmy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Aug 05, 2010 1:16 am
Current Project: A Game
Favorite Gaming Platforms: PS3, PC ( Mac OS )
Programming Language of Choice: C++
Location: New Zealand
Contact:

Re: Freeing memory

Post by Trimmy »

X Abstract X wrote:Assuming that the dynamically allocated memory needs to remain allocated for the lifetime of the object, it should be freed in the destructor, that's what destructors are for.
Yeah thats what I thought but I wasn't sure, thanks.
My Programming Projects:http://www.youtube.com/user/TrimmyS
Post Reply