Problem with dropping text

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
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Problem with dropping text

Post by suliman »

Hi

I get unhandled exception when exiting my game (goes to IReferenceCounted.h to a section commented "someone is doing bad reference counting.").

The problem comes only when i run this function:

Code: Select all

void gameGFX_irr::writeWrap(int flags, float x, float y, int align, float width, const char * fmt, ...){
 
    x+=offsetX;
    y+=offsetY;
 
    bool centered=false;
    if(flags==TEXT_CENTER)
        centered=true;
 
    const int maxLetters=2048;
 
    //prep string
    char txt[maxLetters];
    va_list va;
    va_start( va, fmt );
    vsprintf( txt, fmt, va );
    va_end( va );
    core::stringw str = txt;  
 
    const core::stringw strw(txt);
    const wchar_t* w_str = strw.c_str(); 
    irr::gui::IGUIStaticText * myText;
    myText=myDevice->getGUIEnvironment()->addStaticText(w_str,rect2d(x,y,x+width,screenY),0,true);
    myText->enableOverrideColor(true);
    myText->setOverrideColor(TEXTCOLOR);
    myText->setOverrideFont(font);
    myText->draw();
    myText->drop(); //fix this, floods memory if not dropped but if dropped gets me the problem described above
}
So i need to drop it, but then i get error when exiting my game... Any idea?
Thanks
Erik
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Problem with dropping text

Post by CuteAlien »

Yeah - do not drop it. To remove myText from being displayed you call myText->remove() which will remove it from the GUI. Or if you don't want to create&destroy the object constantly you can also just hide it with myText->setVisible(false).

You only use drop() when you created the object with a function starting with the wort 'create' or when you did call grab() on the object before. There is one more exception when you derive your own classes from the Irrlicht IReferenceCounted interface - in that case you also need to call drop() when you created objects with new, but as long as you only work with Irrlicht's classes you don't have to think about that. grab() & drop() are about memory management - it's not about adding/removing stuff to other classes - like the SceneManager or the GUIEnvironment. If those classes need to grab() drop() objects while doing their job they will do that themself. So the rule for using drop() is really as simple as just mentioned - never use it otherwise.
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
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Re: Problem with dropping text

Post by suliman »

Thanks Cute!

That did the trick indeed.
Post Reply