Dropping a texture causes segmentation fault.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
neil_123
Posts: 67
Joined: Wed Apr 07, 2010 8:50 pm

Dropping a texture causes segmentation fault.

Post by neil_123 »

Hi,

I wanted to display an image, and after displaying the image I wanted to free the memory, (so that I can use it to display some other image).

Here is what I did,

Code: Select all


//
//
// g++ -I/home/laeeq/irrl/irrlicht-1.7.1/include -L/home/laeeq/irrl/irrlicht-1.7.1/lib/Linux   tt_2.cc -lIrrlicht -lGL -lXxf86vm -lXext -lX11
//
//


#include <irrlicht.h>
#include <unistd.h>


using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

IrrlichtDevice *device;
IVideoDriver   *driver;
ITexture       *tex_1;


int main()
{
  int i;
  
  device = createDevice(EDT_OPENGL, dimension2d<u32>(640, 480) );
  
  if(!device)
  {
    printf("%s:%u unable to create device\n", __FILE__, __LINE__);
    return 1;
  }
  
  driver = device->getVideoDriver();

  
  while(device->run() && driver)
  {
    sleep(1);
    printf("creating texture.\n");

    tex_1 = driver->getTexture("/home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png");

    driver->beginScene(true, true, SColor(255, 120, 102, 136) );

    driver->draw2DImage(tex_1, position2d<s32>(0, 0), rect<s32>(0, 0, 443, 250), 0, SColor(255, 255, 255, 255), true);
                                                    
    driver->endScene();

    sleep(1);
    printf("Going to drop\n");
    tex_1->drop();    
    sleep(1);
    printf("dropped\n");
  }
  

  return 0;
}
The program does 3 or 4 iterations, but then it crashes with segmentation fault, here is the output of the program,

Code: Select all

creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
Going to drop
dropped
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
Going to drop
dropped
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
Going to drop
dropped
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
Going to drop
dropped
creating texture.
Segmentation fault

If I remove the line

Code: Select all

    tex_1->drop();    
then the program runs without any problem. Can you tell me what I am doing wrong.

Let me confess, this is my attempt at learning about basics of textures, I am coming from an SDL background, which doesn't have any notion of texture.
neil_123
Posts: 67
Joined: Wed Apr 07, 2010 8:50 pm

Post by neil_123 »

Is it true that,

if I do,

Code: Select all

    tex_1 = driver->getTexture("/home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png");
and do this again,

Code: Select all

    tex_1 = driver->getTexture("/home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png");

then this NOT a memory leak? Irrlicht some how realizes that both these textures are really same, and doesnt really allocate new memory?

Sorry I am too confused.
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

Yes, Irrlicht loads a texture only once.
Not sure why it crashes, though.
Never take advice from someone who likes to give advice, so take my advice and don't take it.
neil_123
Posts: 67
Joined: Wed Apr 07, 2010 8:50 pm

Post by neil_123 »

Hi Bate,

Can you explain what you meant when you said,
Yes, Irrlicht loads a texture only once.
If I execute following code,

Code: Select all

ITexture *tex;

tex = driver->getTexture("file_1.jpg");

// And then I do
tex = driver->getTexture("file_2.jpg");

Where will the pointer tex be pointing in the end? Will it be pointing to file_1.jpg texture, or file_2.jpg texture?

Sorry, if these questions are too basic, I feel I am missing something fundamental here.

Best regards,
Lil Margin
Posts: 212
Joined: Sun Jul 19, 2009 4:24 am
Location: Netherlands Antilles, Curacao

Post by Lil Margin »

you cant drop tex cause its a pointer, just assign 0 to tex...

I may be wrong tho.
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

Don't drop pointers returned by any Irrlicht function except those that start with the word "create..." Irrlicht keeps track texture memory management internally, and if you drop the textures, you screw up the reference count, making Irrlicht delete the texture before it should. It does this so that if you try to load a texture multiple times, it automatically recognizes that it already has a copy stored in its texture cache. To actually free a texture from memory, use IVideoDriver::removeTexture.
neil_123
Posts: 67
Joined: Wed Apr 07, 2010 8:50 pm

Post by neil_123 »

Thanks slavik262,

I replaced the line

Code: Select all

    tex_1->drop();    
with this line,

Code: Select all

    driver->removeTexture(tex_1);

but this results in segmentation fault right when removeTexture() is called.

May be slavik, your technique of copying image to texture is the way to go. Please give me some initial steps or post the code when you have some time.
Best Regards
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

That's not related to the problem. Can we see your code as it is now?
neil_123
Posts: 67
Joined: Wed Apr 07, 2010 8:50 pm

Post by neil_123 »

Here is the program,

Code: Select all


//
//
// g++ -I/home/laeeq/irrl/irrlicht-1.7.1/include -L/home/laeeq/irrl/irrlicht-1.7.1/lib/Linux   tt_2.cc -lIrrlicht -lGL -lXxf86vm -lXext -lX11
//
//


#include <irrlicht.h>
#include <unistd.h>


using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

IrrlichtDevice *device;
IVideoDriver   *driver;
ITexture       *tex_1;


int main()
{
  int i;
  
  device = createDevice(EDT_OPENGL, dimension2d<u32>(640, 480) );
  
  if(!device)
  {
    printf("%s:%u unable to create device\n", __FILE__, __LINE__);
    return 1;
  }
  
  driver = device->getVideoDriver();

  
  while(device->run() && driver)
  {
    sleep(1);
    printf("creating texture.\n");

    tex_1 = driver->getTexture("/home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png");

    printf("ref count just after creation = %d\n", tex_1->getReferenceCount());

    driver->beginScene(true, true, SColor(255, 120, 102, 136) );

    driver->draw2DImage(tex_1, position2d<s32>(0, 0), rect<s32>(0, 0, 443, 250), 0, SColor(255, 255, 255, 255), true);
                                                    
    driver->endScene();

    sleep(1);
    printf("Going to remove\n");

    // tex_1->drop();    
    driver->removeTexture(tex_1);
    
    printf("ref count after dropping = %d\n", tex_1->getReferenceCount());
    sleep(1);
    printf("dropped\n");
  }
  

  return 0;
}


And, here is the output of the program,

Code: Select all

Irrlicht Engine version 1.7.1
Linux 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686
Creating X window...
Visual chosen: : 211
Using renderer: OpenGL 2.1
Mesa DRI Intel(R) Q45/Q43 GEM 20091221 2009Q4 x86/MMX/SSE2: Tungsten Graphics, Inc
OpenGL driver version is 1.2 or better.
GLSL version: 1.2
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
ref count just after creation = 1
Going to remove
ref count after dropping = 64
dropped
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
ref count just after creation = 1
Going to remove
ref count after dropping = 16777472
dropped
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
ref count just after creation = 1
Going to remove
ref count after dropping = 0
dropped
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
ref count just after creation = 1
Going to remove
Segmentation fault
Some times it crashes in first iteration itself, some times it does a few iterations before crashing. Here is the output when it crashed in first iteration itself,

Code: Select all


laeeq@laeeq-desktop:~/extra_ptn/tex_t$ ./a.out 
Irrlicht Engine version 1.7.1
Linux 2.6.32-21-generic #32-Ubuntu SMP Fri Apr 16 08:10:02 UTC 2010 i686
Creating X window...
Visual chosen: : 211
Using renderer: OpenGL 2.1
Mesa DRI Intel(R) Q45/Q43 GEM 20091221 2009Q4 x86/MMX/SSE2: Tungsten Graphics, Inc
OpenGL driver version is 1.2 or better.
GLSL version: 1.2
creating texture.
Loaded texture: /home/laeeq/multi_media/png_seq/FCAJ_BigWin/FCAJ_BigWin_025.png
ref count just after creation = 1
Going to remove
Segmentation fault
laeeq@laeeq-desktop:~/extra_ptn/tex_t$ 
That's not related to the problem. Can we see your code as it is now?
You are right about that slavik262, but I am still interested in knowing how you directly manipulated the texture.[/code]
Post Reply