-3

i have created this class for mesh loading it works but i added this new function to help speed up the loading. when i call the function my program breaks/stops.
here is my function

bool CXFileEntity::LoadXFile(const std::string &filename, int startAnimation, CXFileEntity *entity, LPDIRECT3DDEVICE9 d3ddev)
{
    // We only support one entity so if it already exists delete it
    if (entity)
    {
        delete entity;
        entity=0;
    }

    // Create the entity
    entity=new CXFileEntity(d3ddev);
    if (Load(filename))
    {
        delete entity;
        entity=0;
        return false;
    }

    SetAnimationSet(startAnimation);

    return true;
}
Ramilol
  • 3,394
  • 11
  • 52
  • 84
  • Breaks/stops where? In this function? Another function? – dreamlax Nov 09 '10 at 00:04
  • 5
    Off-topic: What happens when `Load` throws an exception? You have a `delete` in your code, that's bad. Like I've said before, you need to **stop** and get a beginner not-game-oriented C++ book and learn good C++ *first*. Also, `delete 0;` is perfectly fine, everything above `// Create the entity` should be replaced with `delete entity; entity = 0;`. (Though, again, it shouldn't even be there; it should be something along the lines of `entity.reset()`, where `entity` is a smart pointer.) – GManNickG Nov 09 '10 at 00:05
  • you need to give more info mate. like, what errors are you getting when the program crashes – thecoshman Nov 09 '10 at 00:07
  • sorry guys the problem wasn't in there – Ramilol Nov 09 '10 at 00:17

1 Answers1

1

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;
...
Dan Breslau
  • 11,472
  • 2
  • 35
  • 44