new object() question<SOLVED>

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
mv2112
Chaos Rift Junior
Chaos Rift Junior
Posts: 240
Joined: Sat Feb 20, 2010 4:15 am
Current Project: Java Tower Defence Game
Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
Programming Language of Choice: C/++, Java
Location: /usr/home/mv2112
Contact:

new object() question<SOLVED>

Post by mv2112 »

If you have an object:

Code: Select all

class object
{
private:
BITMAP * image;
public:
object(std::string);
};
and you do this in your code:

Code: Select all

object * me;
me=new object(fname);
Will the bitmap be destroyed when you do this, or will create a memory leak?

Code: Select all

delete me;
Last edited by mv2112 on Sun Mar 28, 2010 8:09 pm, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: new object() question

Post by avansc »

mv2112 wrote:If you have an object:

Code: Select all

class object
{
private:
BITMAP * image;
public:
object(std::string);
};
and you do this in your code:

Code: Select all

object * me;
me=new object(fname);
Will the bitmap be destroyed when you do this, or will create a memory leak?

Code: Select all

delete me;
you need to add a deconstructor, ~object();

if you allocate memory for image, you need to delete or free it in the destructor. if you dont do that you will have a leak.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: new object() question

Post by Falco Girgis »

mv2112 wrote:If you have an object:

Code: Select all

class object
{
private:
BITMAP * image;
public:
object(std::string);
};
and you do this in your code:

Code: Select all

object * me;
me=new object(fname);
Will the bitmap be destroyed when you do this, or will create a memory leak?

Code: Select all

delete me;
You don't need your own destructor unless you explicitly did any special memory allocation (on the heap). Your code should be fine, because the compiler supplies the default destructor.

BUT. And this is a big ass but...
As avansc said, something like an image is not only loaded into main RAM, but also video RAM. You must free things in VRAM explicitly. In SDL it's SDL_FreeImage() (or some shit), on the Dreamcast it's pvr_mem_free(). Whatever your video RAM equivalent of free()/delete is that is provided by your API had damn well better be called on any textures existing in VRAM.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: new object() question<SOLVED>

Post by programmerinprogress »

I just thought I would just add on to what has already been said.

If you simply call delete on the pointer to the object of class object, and you haven't written a destructor when you've allocated memory to pointer within the object, then it just deletes the pointers (not the allocated memory) and the members that make up the object you just deleted, which of course would create a memory leak, this is solved by calling the API's specific memory freeing function, e.g. SDL_FreeSurface() for SDL_Surface* to any dynamically allocated objects you created during the life of the parent object. (thus the need for the destructor!)

I had a bit of trouble wording that right,I had to substitute the phrase object quite a lot for that to make sense, so when I said member I was referring to an object within the larger class called object, if I've confused the issue when I was trying to simplify it, then I apologise, and just take on board the very good explanations offered above. ;)
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Post Reply