If I use delete or ::delete I get a C++ exception. Of course I can just let it go without deletion and it is fine, but that memory leak is going to build up very quickly.
My code is as follows: (including anything related to gdi+ that may be relevant)
#include <windows.h>
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
namespace Gdiplus
{
using std::min;
using std::max;
}
#include <gdiplus.h>
#pragma comment (lib,"Gdiplus.lib")
namespace Infra
{
namespace Color
{
//more app code
void CrashingMethod(...)
{
Gdiplus::Bitmap* bitmap = Gdiplus::Bitmap::FromFile(PNG_PATH);
//read bitmap
int qpWidth = frameWidth / 16;
Gdiplus::Color color = Gdiplus::Color();
for (unsigned int y = 0, qy = (frameHeight / 16) - 1; y < frameHeight; y += 16, qy--)
{
for (unsigned int x = 0, qx = 0; x < frameWidth; x += 16, qx++)
{
bitmap->GetPixel(x, y, &color);
byte red = color.GetRed();
//do stuff with the red channel
}
}
delete bitmap; //this line will randomly crash. Not always, not in all machines
bitmap = NULL;
}
}
}
}
What is the right way to deallocate this one? I know there are issues with deleting gdi+ bitmaps, but the solution of doing ::delete doesn't make it any better. Unlike solutions I have seen, I am not using new to create the bitmap, but "FromFile".
Also, as you can see, I am not using I am not using "namespace Gdiplus" so the namespace is not in the scope and I need to refer to it explicitly(which may change things).