OwnedArray.move() problems


#1

EDIT: Doh! false alarm… I had the debugger set to break on anything and it was giving me some random error on startup that was completely unrelated to this… :stuck_out_tongue:


The following gives me an error on execution… :?

OwnedArray<Image> array; array.add(new Image(50,50,false)); array.add(new Image(50,50,false)); array.add(new Image(50,50,false)); array.add(new Image(50,50,false)); array.add(new Image(50,50,false)); array.move(2,1);

Can someone confirm?


#2

Ok then again maybe not. On my laptop (win 2000), running that program causes all kinds of corrupted normal block errors in the memmove function. On my desktop (xp) it runs fine.


#3

…oooh

Just looked at the move() method, and I was clearly drunk when I wrote it, as it’s trying to do a negative sized memmove. Sorry about that, very stupid of me.

Try this:

[code] void move (const int currentIndex,
int newIndex) throw()
{
if (currentIndex != newIndex)
{
lock.enter();

        if (currentIndex >= 0 && currentIndex < numUsed)
        {
            if (newIndex < 0 || newIndex >= numUsed)
                newIndex = numUsed - 1;

            ObjectClass* const value = this->elements [currentIndex];

            if (newIndex > currentIndex)
            {
                memmove (this->elements + currentIndex,
                         this->elements + currentIndex + 1,
                         (newIndex - currentIndex) * sizeof (ObjectClass*));
            }
            else
            {
                memmove (this->elements + newIndex + 1,
                         this->elements + newIndex,
                         (currentIndex - newIndex) * sizeof (ObjectClass*));
            }

            this->elements [newIndex] = value;
        }

        lock.exit();
    }
}

[/code]


#4

(And there should be similar fixes for Array, ReferenceCountedArray, etc…)


#5

That fixed it, thanks! :smiley:


#6