English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi,

Im having some memory leak problems, im creating objects of classes which display on screen using SDL and play sound using FMOD. I then delete the object using delete and create a new object that does similiar, eg.

#include "SDL.h"
#include "menu.h"
#include "game.h"

// main, run menu screen, then run game.

int main( int argc, char* argv[] )
{

int loop=1;

while(loop){
menu *m = new menu();
if(m->run()==1){
delete m;
Game *g = new Game();
delete g;
}//if player chooses start 1 will be returned and the game will start else the game will exit
else loop=0;//end
}

return ( 0 ) ;
}

the objects dont seem to be getting deleted and the memory increases each time.

any advice how i can make sure that the objects are deleted completly, including everything the object holds.

2006-12-06 09:07:43 · 5 answers · asked by karljj1 4 in Computers & Internet Programming & Design

5 answers

The problem is the menu pointer is not always being deleted.

The menu pointer (m) is getting deleted only when run() returns 1. You need to move the delete statement for the m variable outside of the if statement and before the end of the loop, like this:

while(loop)
{
menu *m = new menu();
if(m->run() == 1)
{
Game *g = new Game();
delete g;
}
else
loop = 0;

delete m;
}

2006-12-06 12:52:34 · answer #1 · answered by ballarke 3 · 1 0

In C you have not any sensible way of monitoring pointer references. you should do sensible issues in C++ that music references, although those are literally not oftentimes used to discover memory leaks. except for as those issues typically grant you with get admission to to the underlying pointer they're typically defeated in unpredicted techniques. once you've each and each and every of the scaffolding to hit upon even as memory is unreferenced, you could besides only delete it. Languages like Java and C# deal with their memory in this way. seen C++, and probably different C compilers, do grant equipment for debugging memory leaks. those artwork with techniques from allowing you to take a photo of what memory is allotted, run some code that supposedly allocates and deallocates memory and then examine that no memory allotted after the photo remains allotted. without imposing finished style safe practices in a language you could not hit upon memory leaks. evaluate right here C, does the memory get leaked and unleaked? int n = (int)malloc(sizeof(int)) + 12345; /* we've not pointer to the memory right here, only an integer that has some courting to the memory we allotted */ int* p = (int*)(n -12345); loose(p);

2016-11-30 05:42:39 · answer #2 · answered by ? 4 · 0 0

delete just deletes what a particular pointer is pointing to, for an object you would need to declare a deconstructor, I will provide an example below. When the object goes in scope the default constructor will be called, when the object goes out of scope the deconstructor will be called.

//Class definition

class fakeclass
{
fakeclass(); //defualt Constructor
~fakeclass(); //object Deconstructor
private:
int *datamember;
};

//function definitions
//default constructor
fakeclass :: fakeclass
{
datamember = NULL;
//code to initialize data members to empty state
}
//class deconstructor
fakeclass :: ~fakeclass
{
delete datamember;
//code to destroy what you want deleted on exit.
}

2006-12-06 09:26:15 · answer #3 · answered by D 4 · 1 0

What metrics seem to indicate a memory leak?

2006-12-06 12:54:45 · answer #4 · answered by rons_brain 2 · 0 1

ok
i'll look into that!
sorry don't know...ther is a command for that i just can't remember..but this is a common problem..maybe u should seek help from a pro

2006-12-06 09:15:21 · answer #5 · answered by zoli_zly 3 · 0 3

fedest.com, questions and answers