I am using Irrlicht to draw an image to the screen using the code below. I basically get the image data from my drawing class, convert it to an IImage, convert that to a second scaled up IImage, then convert that to a ITexture, which gets drawn to the screen. Obviously if I run this code without deleting or dropping stuff I get a bad memory leak, but I noticed weird behavior when dropping the texture. If I place tex->drop() after the scene drawing code at the end, the program crashes after just a few seconds. If I drop the texture before running the scene drawing code, the program will run forever with no apparent memory leak UNTIL I click on any other window. Clicking on another window will cause the program to crash. I can click on the program's window, move it around, etc, but clicking any other window causes a crash. If I remove the tex->drop() line altogether, the program will tolerate clicking on other windows, but it causes a bad memory leak.
// get image data, convert to IImage and scale image to 640 by 480
unsigned char * ppm_data = ppm.get_image_data();
irr::video::IImage* image = driver->createImageFromData(irr::video::ECF_R8G8B8, irr::core::dimension2d<unsigned int>(320, 240), ppm_data);
irr::video::IImage* image2 = driver->createImage(irr::video::ECF_R8G8B8, irr::core::dimension2d<unsigned int>(640, 480));
image2->fill(irr::video::SColor(255, 255, 0, 0));
image->copyToScaling(image2);
// convert the scaled image to an ITexture
irr::video::ITexture* tex = driver->addTexture("NAME", image2);
// delete the image data
delete[] ppm_data;
ppm_data = nullptr;
image->drop();
image2->drop();
tex->drop(); // <-- This line fixes the memory leak but causes crashes when window loses focus
// draw scene...
driver->beginScene(true, true, irr::video::SColor(250, 190, 190, 250));
driver->draw2DImage(tex, irr::core::vector2d<int>(0, 0));
driver->endScene();
Any suggestions on what I'm doing wrong? How do I get the program to tolerate clicking on other windows without crashing?