Huge memory leak when restarting Irrlicht...

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.
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

Sounds good, whats the function call to get the total count?
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Huge memory leak when restarting Irrlicht...

Post by CuteAlien »

We're talking about maybe adding a feature, so obviously there can't be a function call yet.
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
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

So I could just add a global counter and increment it in the grab function..
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Huge memory leak when restarting Irrlicht...

Post by serengeor »

robmar wrote:So I could just add a global counter and increment it in the grab function..
yeah, that's the idea
Working on game: Marrbles (Currently stopped).
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

Okay, here are the details of my code in which I seem to have a memory leak somewhere.

From the head file for getMesh, it says not to drop the returned mesh pointer:-

virtual IAnimatedMesh* getMesh(const io::path& filename) = 0;
....
//! Get pointer to an animateable mesh. Loads the file if not loaded already. * \return Null if failed, otherwise pointer to the mesh.
* This pointer should not be dropped. See IReferenceCounted::drop() for more information.
**/
virtual IAnimatedMesh* getMesh(const io::path& filename) = 0;

So I don´t drop it, but before reloading a new scene from a file, I remove the meshes. Strangely, aftercalling getMeshCount and removing that number of meshes, when calling getMeshCoutn again, there are still meshes to delete!!! So I have to run the remove loop again! Is there an explication for this behaviour?

Code: Select all

 
        // Ensure all 3D resources freed
        if ( m_pDriver )
        {
                // Delete all meshes
                if ( m_pSmgr )
                {
                        IAnimatedMesh *pMesh;
                        int i, iMl = 0;
                        IMeshCache* pMeshCache = m_pSmgr->getMeshCache();
                        if ( pMeshCache )
                        {
dml:            s32 iMeshCount = pMeshCache->getMeshCount();
                                for ( i = 0; i < iMeshCount; i++ )
                                {
                                        pMesh = pMeshCache->getMeshByIndex( i );
                                        if ( pMesh )
                                                pMeshCache->removeMesh( pMesh );
                                }
 
                                if ( iMl < 10 && pMeshCache->getMeshCount() )
                                {
                                        iMl++;
                                        goto dml;
                                }
                        }
                        else
                        {
                                ASSERT( false );
                                return -1;
                        }
                        m_pMeshCube = NULL;                             // Has now been deleted
                }
 
 
CuteAlien
Admin
Posts: 9628
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Huge memory leak when restarting Irrlicht...

Post by CuteAlien »

Problem is this line:

Code: Select all

 
pMesh = pMeshCache->getMeshByIndex( i );
 
If you remove the first element - the next element has now index 0.
So what you should use here is not i but 0, then all elements will be removed. If you start removing from the back it will be even faster - and then you can use an index (just be careful not to use an unsigned index and check for >= 0 ).
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
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

:oops: Oh no.... not again, I made that mistake last week too!!

So thanks, um, just popping out to jet wash my neurons!! :oops:

This is the new code, removing from back to avoid causing the internal copy-down:

Code: Select all

 
        u32 uMeshCount = pMeshCache->getMeshCount();
                        for ( u32 ui = uMeshCount; ui > 0; ui-- )
                        {
                                pMesh = pMeshCache->getMeshByIndex( ui-1 );
                                if ( pMesh )
                                        pMeshCache->removeMesh( pMesh );
                        }
 
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Huge memory leak when restarting Irrlicht...

Post by hendu »

Just curious, why not use ->clear()?
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

Good question... if a mesh has more than one grab on it, clear won´t delete it, and then it won´t clear up the array allocations. So I am trying to do this in a controlled way, and handle multiple grabs.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Huge memory leak when restarting Irrlicht...

Post by serengeor »

You shouldn't have it grabbed multiple times in the first place before clearing, cause if it is grabbed by some class, that class expects it to still be available until it drops it.
Working on game: Marrbles (Currently stopped).
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

Yes of course, but the thing is to know this has happened so the code can be fixed, which clear doesn´t report.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Huge memory leak when restarting Irrlicht...

Post by hendu »

clear()
getMeshCount()
?
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

yes, that works! I was just testing
robmar
Posts: 1125
Joined: Sun Aug 14, 2011 11:30 pm

Re: Huge memory leak when restarting Irrlicht...

Post by robmar »

I´ve also found a massive memory leak with the opengl driver even on the beta 1.8.0 driver, around 12+ MB on closing, which does not occur when using the D3D driver.

Win 7 32-bit, Irrlicht 1.7.2. through to 1.8.0 SVB 4309
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Huge memory leak when restarting Irrlicht...

Post by hendu »

1.8 final is out ;)
Post Reply