memory leak due to pointer in function?

on another forum, someone said I had a memory leak. He didn't specify how or why.
Referring to my last thread, I had a pointer being passed from a method. I thought a way to protect against losing control of a pointer was to pass a dummy pointer in its place:
(using hypothetical functions)
bitarray already initialized, declared in header
bitmap* getthing()
{
bitmap *a = copy_bitmap(bitarray[num]);
return a;
}
when getthing is called, it gets used in another function in another program:
blit(thing.getthing(),source_bitmap,x,y,size_x,size_y); //blit paints a copy of bitmap thing.getthing() onto source_bitmap

I've long since removed this function.

the only thing I can guess the memory leak is coming from is when getthing is called, a falls out of scope, which means it gets released, causing an unaligned or null pointer to be passed to function blit
Sounds like you have some misconceptions about how memory management works in C++...
since you don't store the return value of getthing() anywhere, the pointer to the bitmap is lost. The bitmap still exists in memory, but you don't have any way of deleting it now that you lost the pointer.
It would have been different if getthing() returned a smart pointer.
Topic archived. No new replies allowed.