Page 1 of 1

[fixed]irr::list copy constructor and operator=

Posted: Thu Jun 22, 2006 12:12 am
by CuteAlien
This is not necessarly a bug, but they work different than std::list so i thought i mention it. I think the std::list does not copy the nodes but only the elements of a list.
The difference can be seen in the following behaviour:

Code: Select all

    std::list<int> stdA;
    stdA.push_back(1);
    std::list<int> stdB(stdA);
    std::list<int> stdC;
    stdC = stdA;

    core::list<int> irrA;
    irrA.push_back(1);
    core::list<int> irrB(irrA);
    core::list<int> irrC;
    irrC = irrA;
core::list will crash in both cases in the destructor because after copying the list-nodes will be referenced by both lists.
I'd recommend changing it, but i'm not sure if there ain't already code in the engine expecting that behavior...

Posted: Thu Jun 22, 2006 6:13 am
by vitek
Yet another reason for Irrlicht to just use the Standard C++ Library.

Posted: Thu Jun 22, 2006 8:14 am
by hybrid
Indeed there are already some bug reports in the SF tracker showing severe problems due to this copy constructor. Since Irrlicht containers are meant to replace STL containers they should copy the complete behavior.

Posted: Fri Jun 23, 2006 2:39 pm
by CuteAlien
Simple Bugfix in irrList.h:

Code: Select all

	//! Copy constructor
	list(const list<T>& other)
	{
		*this = other;
	}

    //! Assignement operator
	void operator=(const list<T>& other)
	{
        root = 0;
        last = 0;
        size = 0;

        SKListNode* node = other.root;
		while(node)
		{
		    push_back(node->element);
			node = node->next;
		}
	}

Posted: Sat Jul 15, 2006 3:07 am
by game
I have a segmentation fault in the destructor when I want to use this class in my own class:

Code: Select all

typedef irr::core::array<irr::scene::IAnimatedMesh*> listM;
class arrayM
{
    public:
        arrayM(){is_init=false;}
        virtual ~arrayM(){}
        void init(irr::s32 x1,irr::s32 y1)
        {
            lm.empty();
            x=x1;
            y=y1;
            max=x*y;
            current=0;
            is_init=true;
        }
        void add_item(irr::scene::IAnimatedMesh* mesh)
        {
            if(is_init)
                lm.push_back(mesh);
        }
        irr::scene::IAnimatedMesh* get_item(irr::s32 x1,irr::s32 y1)
        {
            if(!is_init)
                return 0;
            return lm[((y1-1)*x+x1)];
        }
    private:
        listM lm;//le plus important

        irr::s32 x;
        irr::s32 y;
        irr::s32 max;
        irr::s32 current;

        bool is_init;
};
Is anybody can help me ? :?

I forgot an important information: i'm using this class in an array like this"arrayM arr[9]" else if it is not in an array there is no problem!