Will this cause problems?

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
utunnels
Posts: 37
Joined: Thu Apr 05, 2012 2:52 pm

Will this cause problems?

Post by utunnels »

Code: Select all

class SomeClass
{
    SMesh Mesh;
    SMeshBuffer MeshBuffer[3];
 
public:
 
    SomeClass()
    {
        Mesh = SMesh();
        for(int i=0; i<3; i++)
        {
            MeshBuffer[i] = SMeshBuffer();
            Mesh.addMeshBuffer(&MeshBuffer[i]);
            // Will this cause any problem when ~SMesh() is called?
        }
    }
 
    ~SomeClass()
    {
        // Should I do something here?
    }
    
};

Or should I new mesh and mesh buffers to avoid the trouble?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Will this cause problems?

Post by hybrid »

Yes, all IReferenceCounted objects should be created on the heap, i.e. using new.
utunnels
Posts: 37
Joined: Thu Apr 05, 2012 2:52 pm

Re: Will this cause problems?

Post by utunnels »

Thank you for the tip. :)
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Will this cause problems?

Post by chronologicaldot »

What hybrid said. Furthermore, in your deconstuctor (~SomeClass() ), you can call "Mesh->drop()" (which handles deletion), but for your usage, it looks like "delete Mesh" would work.
utunnels
Posts: 37
Joined: Thu Apr 05, 2012 2:52 pm

Re: Will this cause problems?

Post by utunnels »

I see, thank you.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Will this cause problems?

Post by hybrid »

Yeah, but once you gave the pointer to the mesh somewhere else, it might be grabbed by the external entity. So always do drop() for classe based on IReferenceCounted. And also make sure that you call drop on the meshbuffers right after you called addMeshBuffer. This will ensure that onyly the SMesh holds a reference (and not the surrounding class) and thus everything wil be cleaned up properly.
Post Reply