[Solved] c++ breaks on class function


EDIT: Wait… I hadn’t realized that this function is a member of the CXFileEntity class. It doesn’t look like it’s a static method, either. So in order to call this function, you already need to have instantiated a CXFileEntity object! Therefore, it’s likely that you absolutely do not want to be either deleting or creating CXFileEntity objects inside of this method. (If you truly only allow one entity to exist at a time, you’ll be effectively deleting ‘this’ and then trying to re-create it. That doesn’t work, no way, no how.)

I’m leaving the earlier answer in place in hopes that it will still provide you some clue about how pointers work.


You’d do better to give more information, such as where and how the program breaks.

But this is clearly wrong:

CXFileEntity *entity, 

because it means that the new object allocated by

entity=new CXFileEntity(d3ddev);

will not be seen by the caller. (entity is a local variable, so changes to it won’t be seen outside of the local scope.)

Try changing the code to pass entity as a pointer to a pointer:

CXFileEntity **entity, 

which will mean changing the code inside the function to match:

if (*entity)
{
    delete *entity;
    *entity=0;
}

// Create the entity
*entity=new CXFileEntity(d3ddev);

// etc.

You’ll also have to change the caller to pass a pointer to a pointer. And for goodness’ sake, make sure that the first time you pass the pointer in, it’s initialized to 0:

CXFileEntity *the_entity = 0;
...

solved c++ breaks on class function