[no bug]heapsort invalid pointer

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
henko
Posts: 6
Joined: Mon Sep 19, 2011 3:08 pm

[no bug]heapsort invalid pointer

Post by henko »

Using: Irrlicht branch 1.7, revision 3926

I'm chasing a bug for hours now. My program started to crash when i'm calling

Code: Select all

driver->addTexture(name, image);
The debugger shows that access violation happens when irrlicht tries to sort my textures after adding the new one.

Code: Select all

//! adds a surface, not loaded or created by the Irrlicht Engine
void CNullDriver::addTexture(video::ITexture* texture)
{
        if (texture)
        {
                SSurface s;
                s.Surface = texture;
                texture->grab();
 
                Textures.push_back(s);
 
                // the new texture is now at the end of the texture list. when searching for
                // the next new texture, the texture array will be sorted and the index of this texture
                // will be changed. to let the order be more consistent to the user, sort
                // the textures now already although this isn't necessary:
 
                Textures.sort();
        }
}
The sort is done by heapsort which is like this:

Code: Select all

//! Sorts an array with size 'size' using heapsort.
template<class T>
inline void heapsort(T* array_, s32 size)
{
        // for heapsink we pretent this is not c++, where
        // arrays start with index 0. So we decrease the array pointer,
        // the maximum always +2 and the element always +1
 
        T* virtualArray = array_ - 1;
        s32 virtualSize = size + 2;
        s32 i;
 
        // build heap
 
        for (i=((size-1)/2); i>=0; --i)
                heapsink(virtualArray, i+1, virtualSize-1);
 
        // sort array, leave out the last element (0)
        for (i=size-1; i>0; --i)
        {
                T t = array_[0];
                array_[0] = array_[i];
                array_[i] = t;
                heapsink(virtualArray, 1, i + 1);
        }
}
It says that virtualArray is an invalid pointer which seems true considering that the original array pointer is decreased by one. Which means that it will point before the array to an invalid memory. I don't know what is the reason for this, but it seems wrong to me.

Image

Any thoughts? Where else could this bug come from? It seems that the < operator fails to compare the names in irrString.h, because one name has an invalid pointer inside.

Image
Image
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: heapsort invalid pointer

Post by CuteAlien »

Did you maybe release one of the textures you added before?
I'm sorry, but that is not enough information, you have to give us something which we can reproduce to find such problems.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
henko
Posts: 6
Joined: Mon Sep 19, 2011 3:08 pm

Re: heapsort invalid pointer

Post by henko »

After hours of debugging I have found the problem. It's not a bug in irrlicht, so you can remove the topic if you want.

The problem was the following:

I have created a render target by:

Code: Select all

ITexture* target = driver->addRenderTargetTexture(...)
And I though I can drop it when i'm done, so i called:

Code: Select all

target->drop()
But now I know that I have to call:

Code: Select all

driver->removeTexture(target)
instead of dropping it. :)

Sorry for the false bug report. :)
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: heapsort invalid pointer

Post by hybrid »

Yeah, the add* in the method name hints for this behavior :-)
Post Reply