irr::core::map examples

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
Arclamp
Posts: 71
Joined: Thu Oct 10, 2013 7:45 pm

irr::core::map examples

Post by Arclamp »

I'm doing some map examples but am struggling to fully delete, valgrind always says there is an item left behind...

Code: Select all

 
valgrind --leak-check=full --show-reachable=no --track-origins=yes ./play
 

Code: Select all

 
void map_test_01()
{
    
    irr::core::map<s32, stringc*> items;
    
    //  ADD ITEMS
    stringc *so = new stringc("Apples");
    items.insert(33, so);
    
    so = new stringc("Kiwi");
    items.insert(37, so);
    //items.set(31, new stringc("Bananas"));        //  replace / insert
    
    //items[55] = new stringc("Ants");
    
    /*
    //  DUMP
    for(map<irr::s32,stringc*>::Iterator it=items.getIterator(); !it.atEnd(); it++)
    {
        std::cout << it->getKey() << " => " << (*it->getValue()).c_str() << std::endl;
    }
    
    std::cout << "" << std::endl;
    
    //  DUMP
    //irr::core::map<s32, stringc*> items;
    irr::core::map<s32, stringc*>::Iterator it;
    for(it = items.getIterator(); !it.atEnd(); it++)
    {
        std::cout << it->getKey() << " => " << (*it->getValue()).c_str() << std::endl;
    }
    
    std::cout << "" << std::endl;
    
    //  FIND USING ITERATOR
    //irr::core::map<s32, stringc*>::Iterator it = items.find(31);
    it = items.find(55);
    if (!it.atEnd())
    {
        //return (*it).getValue();
        std::cout << "Found: " << it->getKey() << " => " << (*it->getValue()).c_str() << std::endl;
    }
    
    std::cout << "" << std::endl;
    
    //  FIND USING NODE
    core::map<s32, stringc*>::Node* n = items.find(31);
    if (n)
    {
        //indices.push_back(n->getValue());
        std::cout << "Found: " << n->getKey() << " => " << (*n->getValue()).c_str() << std::endl;
    }
    */
    
    
    std::cout << "" << std::endl;
    
    core::map<s32, stringc*>::ParentFirstIterator Iterator=items.getParentFirstIterator();
 
    for(;!Iterator.atEnd();Iterator++)
    {
        
        core::map<s32, stringc*>::Node* n = Iterator.getNode();         //  items.find(31);
        
        
        //delete Iterator.getNode()->getValue();
        //stringc *n = Iterator.getNode()->getValue();
        
        delete n->getValue();
        n->getValue() = NULL;
        /*
        stringc *ss = n->getValue();
        delete ss;
        ss = NULL;
        */
        
        items.remove(n);
        
        
        
        Iterator = items.getParentFirstIterator();
        
        
    }
    
    //items.clear();
    
    
    std::cout << "" << std::endl;
    
}
 
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: irr::core::map examples

Post by CuteAlien »

Use STL map unless you have a really good reason to use Irrlicht map. Irrlicht map simply isn't as good.

Looking at your code - I don't know what getParentFirstIterator really does. Likely using getIterator() would make more sense.
Next - calling remove() will likely invalidate the iterator. This is not documented, but I can't imagine otherwise. Which is why std::map allows erasing with the iterator and returns the next iterator as result. While Irrlich map has not even an erase function with iterator. Which sucks.
So basically - you could delete all your values - and then after the loop call clear() on the map. Or you could delete always getIterator() until it no longer returns a valid iterator. But looping over the map and erasing all elements together with the map-containers won't work.
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
Arclamp
Posts: 71
Joined: Thu Oct 10, 2013 7:45 pm

Re: irr::core::map examples

Post by Arclamp »

Ahhh, cool...

Using because wanted in another element, the original code uses STL. Think I got it compile-wise but somehow polluted IAttributes... (this other thing started off in io:: and then got shifted to gui::, but then again I based this on IAttributes, so who knows where the pollutant is coming from? Time to start afresh :D
Post Reply