I'm relatively new to C++. I'm allocating a buffer:
uint8 *buffer = new uint8[len];
Using a 3rd party library, I use a method of an "img" object (it's a picture) to "take over" the buffer as raw image data:
img->SetBuffer((uint8*)data);
I suspect that "taking over" in practice means that the "img" object has its own pointer which after "SetBuffer" points to the data in "buffer". It all works fine, but my compiler complains (it's a warning, not an error) about a memory leak. If I add this line: "delete buffer;" after SetBuffer, the warning goes away, but at the same time my "img" ends up blank (no data). How do I avoid the compiler warning and keep the data? Is there a way to delete just the "buffer" pointer itself, without destroying the data it points to? Later on in the code, I delete the "img" object, which I guess wipes out all the image data anyway.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…