problems with memory leak

I have no idea what's causing it, because gdb doesn't end where the error occurs it keeps going until it crashes somewhere. Therefore the stacktrace is the same after each run. The program consists of 2 headers with defining .cpp and one main program. Its supposed to be a game that handles images using an external game library. I'm asking here because my problems are more fundamental to C++ than game programming. I have a problem with memory management so I thought I could avoid some problems by doing this:
main:
image *Image;
void draw()
{
anim.update();
anim.draw(*Image);
draw_to_screen(image);
}
anim:
void update()
{
//stuff
}
void draw(image &Image)
{
player1.draw(Image);
player2.draw(Image);
}
player:
void draw(image &Image)
{
draw_on_sprite(sprite,&image,0,0,0,0); //draws sprite onto image
}
I realize that the middle man isn't needed (anymore) I only had it because I wanted to do the animation with double buffering. Assume all of the example methods(draw_to_screen,draw_on_sprite) are flawless. Will the above method be ok? Could it be the source of my memory leak?
image *Image; That is probably the problem.
How would that cause a problem exactly? Just to clarify, all of the external functions like draw_to_screen and draw_on_sprite have image* as their parameter. Also, I thought I could avoid problems by doing:
image one;
image *two = &one;
then using two as the parameter, but it would only work (in main) if I passed *two to anim.draw. Why is that?
also, I noticed despite using the & reference, If I had void function(Image &i)
i would not be recognized as a pointer. I had to use the & reference again. If I have the energy tomorrow I'm going to re-write the program cutting out the middle man and using only player.cpp and main.cpp and see if that helps. Now that I think about it I think I have to pass the pointer as *pointer because as it makes its way to player.cpp its going deeper into the address toward the source, so since its passing through 2 programs I need a double pointer. Does that make any sense?
Last edited on
Nope, it doesn't make sense.

http://cplusplus.com/doc/tutorial/functions2/ there is no pass by reference in C. In order to obtain the same effect, the functions expect a pointer instead. But don't do this crap image *two = &one;

Memory leaks come from using dynamic allocation. So if you avoid that, you wouldn't have leaks.
Topic archived. No new replies allowed.