Jump to content
  • Advertisement
Sign in to follow this  
huaihongwu

how to release the memory???weird

This topic is 3407 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

void GenerateLightnings(int amount, vector3_t startpoint, vector3_t endpoint) { CLightningBolt * lp; for (int i = 0; i < amount; i ++) { lp = new CLightningBolt(5, 0.6f); lp->InitializeLightningBolt(startpoint, endpoint); lp->GenerateLightningBolt(); lp->RenderLightningBolt(); delete []lp; } } hi, guys , i use this function in my gldrawscene loop, but i found that i am running out of memory very soon after i ran the program for a while. how can i release the memory which created by "new". the CLightningBolt is my own class, but i did define any destructor, cause every time i add this in the class, there are lots of weird porblems appeared...... ~CLightningBolt();

Share this post


Link to post
Share on other sites
Advertisement
You can't use "delete[]" on memory that was allocated with "new".

1. If you allocated with "new", use "delete".

2. If you allocated with "new[]", use "delete[]".

3. If you allocated with "malloc()", use "free()".

Share this post


Link to post
Share on other sites
It looks like you want to generate a number of lightning bolts there. Why not put them in a std::vector<> (a dynamic array)?

Tell us more about the errors you had when you included a destructor. If the compiler issued an error message, please copy and paste it into your response.

Share this post


Link to post
Share on other sites
but how do you delete a class. for example, i have this :

class Physecs2D
{
public:
private:
...
};

then i create

Physecs2D gamePhysecs;

i tryed : delete gamePhysecs;
and it did not work!

thanks!

Share this post


Link to post
Share on other sites
@AnnihilatorOrion: that object is not dynamically allocated. It will be destroyed once it goes out of scope. You should only 'delete' what you 'new'.

Besides, delete can only be called on pointers. 'gamePhysecs' is not a pointer.

Share this post


Link to post
Share on other sites

CLightningBolt lp;
lp.DoStuff();



lp doesn't need to be deleted, it will be destroyed automatically when it goes out of scope.


CLightningBolt* lp;
lp = new CLightningBolt();
lp->DoStuff();
delete lp;




lp is now a pointer to a CLightningBolt. The CLightningBolt it points to needs to be deleted when you've done with it.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!