Jump to content
  • Advertisement
Sign in to follow this  
Jungletoe

Destructors for Enemies and Projectiles

This topic is 2518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a simple graphical RPG game using SDL and C++ but ran into some problems a few days ago due to my lack of OOP understanding. Since I learned OOP with Java, I've been having problems with destructors and pointers. One main area where deconstructors are required is with enemies and projectiles. I want the bullets to be deleted when they hit their target and I want enemies to delete when they don't have any health.

I'm just looking for some pseudocode as to how I would avoid memory leaks in regards to this. For example, this is a rough sketch of my enemy class:


class enemy
{
private:
int x, y, width, height;
SDL_Surface* sprite;
SDL_Rect boundingBox;
int health;
public:
enemy(); //constructor 1
enemy(int X, int Y, int W, int H) //constructor 2
~enemy(); //destructor
void setX(int X);
void setY(int Y);
void draw();
void setHealth(int H);
}


The definitions of all those functions and the values of the variables should be assumed. They are all defined in the enemy.cpp, naturally.

With that available code, would I only be able to delete the sprite since it's the only pointer? Should I make all my variables pointers and have them point to different values? What is the most efficient way to do this?

Thanks!
-Brady

Share this post


Link to post
Share on other sites
Advertisement

I'm trying to make a simple graphical RPG game using SDL and C++ but ran into some problems a few days ago due to my lack of OOP understanding. Since I learned OOP with Java, I've been having problems with destructors and pointers. One main area where deconstructors are required is with enemies and projectiles. I want the bullets to be deleted when they hit their target and I want enemies to delete when they don't have any health.

I'm just looking for some pseudocode as to how I would avoid memory leaks in regards to this. For example, this is a rough sketch of my enemy class:


class enemy
{
private:
int x, y, width, height;
SDL_Surface* sprite;
SDL_Rect boundingBox;
int health;
public:
enemy(); //constructor 1
enemy(int X, int Y, int W, int H) //constructor 2
~enemy(); //destructor
void setX(int X);
void setY(int Y);
void draw();
void setHealth(int H);
}


The definitions of all those functions and the values of the variables should be assumed. They are all defined in the enemy.cpp, naturally.

With that available code, would I only be able to delete the sprite since it's the only pointer? Should I make all my variables pointers and have them point to different values? What is the most efficient way to do this?

Thanks!
-Brady


POD types like ints & floats can safely be left as member values. You only need to delete() what you new(), and only need to free() what you alloc().
Along those guides, your sprite member only needs to be deleted if enemy owns the reference - you wouldn't want to delete the pointer twice!

The destructor is something akin to java finalizer methods, with the exception that it has to be manually called as there is no GC system.
The requirements for pointer destruction should be considered in the same way as using a freelist in a garbage collected language - that memory is permenantly reserved until you give it back.

Share this post


Link to post
Share on other sites
To handle manual memory management effectively, you will need control how and when objects and resources are created and destroyed. As such, you will need a different perspective on your design. You will need to determine the object ownership (who is responsible for deleting which objects) and type of ownership (strong or weak) for each object reference. For instance, since you plan to have enemies and bullets removed on some condition, so you will need some sort of manager to determine how and when those objects are removed. This manager will have strong ownerships to the objects it manages, as it decides when objects are created and removed from the game. In another instance, each enemy has a pointer to an SDL_Surface to the image the enemy represents. Of course, it would be silly to reload the same image loaded multiple times into memory, so in this case, the enemies should share a weak pointer to the SDL_Surface, and the SDL_Surface is owned by some other object (the resource manager, perhaps). You can also use smart pointers to make the abstraction clear, and to help manage memory easier.

And, to answer your questions:
Does the enemy need to destroy its sprite? No, because instances of enemy should share the same sprite and the enemy shouldn't have the knowledge of when to destroy/release the sprite resource (the resource can be reused elsewhere).
Do value types need to be destroyed/deleted? No, because deleting something entails that something was allocated via new, malloc, or even SDL_CreateSurface. Members are allocated along with the space that allocated the object.

Hope this helps.

Share this post


Link to post
Share on other sites
Thanks guys! I figured out some stuff about pointers and allocation of memory and I know how to attempt this now.

Also I appologize for once again posting in the wrong section.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!