0

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?

user137
  • 282
  • 2
  • 14
  • It's not clear what tex is - you posted only a part of some code. is tex being used elsewhere ? – darune Apr 30 '19 at 06:44
  • @darune It turns out that I had declared the image, image2, and tex variables outside the while loop, then redeclared them inside the while loop. I removed the second declaration and just reassign the variables inside the loop now. But it doesn't fix the crash issues, in fact the program sometimes crashes on startup now. – user137 Apr 30 '19 at 07:38
  • 1
    well, you need to use a debugger to see why it crashes then. – darune Apr 30 '19 at 08:36

0 Answers0