C ++, why am I getting access violations after changing new objects?

Okay, so I'm working on a game right now and ran into a memory problem after refactoring the code.

It uses a component project, and I changed how the components were distributed and passed to entities. Initially, some components were distributed as member variables within entities, but now I want them to be distributed elsewhere and passed to the entities via a pointer.

You can see how I implemented this below with sample code from my project. I essentially iterate over all the objects and select components for them. The problem is that I find an access violation in the first line of the “launch” “instanceObject” on the 6th iteration and have no idea why. Using the debugger, it does not look like any of the variables points to an invalid address.

Here is what I do to create entities and components.

for (unsigned int i = 0; i < 512; ++i) {
    InstanceObject* _pInstanceObject = new InstanceObject;

    // Initialize temporary variables
    XMFLOAT3 _position, _rotation;
    float _angle = (i / 512.0f) * (2.0f * XM_PI),
          _scale = (float)pResourceManager->numberGenerator.GetInt(50, 5);

    _position.x = 100000.0f * cos(_angle) + pResourceManager->numberGenerator.GetInt(50000, -25000);
    _position.y =(float) pResourceManager->numberGenerator.GetInt(50000, -25000);
    _position.z = 100000.0f * sin(_angle) + pResourceManager->numberGenerator.GetInt(50000, -25000);

    _rotation.x = (XM_PI * 2) * (pResourceManager->numberGenerator.GetInt(100, 0) / 100.0f);
    _rotation.y = (XM_PI * 2) * (pResourceManager->numberGenerator.GetInt(100, 0) / 100.0f);
    _rotation.z = (XM_PI * 2) * (pResourceManager->numberGenerator.GetInt(100, 0) / 100.0f);

    // Set component state using the temporary variables.
    _pInstanceObject->StartUp(&_position,
        &_rotation,
        &XMFLOAT3(_scale, _scale, _scale),
        &XMFLOAT3(0.0f, 0.0f, 1.0f),
        &XMFLOAT3(1.0f, 0.0f, 0.0f),
        &XMFLOAT3(0.0f, 1.0f, 0.0f)
        );

    // Hand pointer of the component to entity.
    // Entity will handle deallocating component
}

And here is the corresponding code from the component.

class InstanceObject {
private:
    XMVECTOR anteriorAxis,
         lateralAxis,
         normalAxis,
         position,
         rotationQuaternion,
         scale;

    XMMATRIX translationMatrix,
         rotationMatrix,
         scaleMatrix;
    void SetAnteriorAxis(const XMFLOAT3 *_anteriorAxis) { anteriorAxis = XMLoadFloat3(_anteriorAxis); }
    void SetLateralAxis(const XMFLOAT3 *_lateralAxis)   { lateralAxis = XMLoadFloat3(_lateralAxis); }
    void SetNormalAxis(const XMFLOAT3 *_normalAxis)     { normalAxis = XMLoadFloat3(_normalAxis); }
public:
    InstanceObject(void)  { }
    InstanceObject(const InstanceObject& _object) : anteriorAxis(_object.anteriorAxis), lateralAxis(_object.lateralAxis),
        normalAxis(_object.normalAxis), position(_object.position), rotationQuaternion(_object.rotationQuaternion), scale(_object.scale),
        translationMatrix(_object.translationMatrix), rotationMatrix(_object.rotationMatrix), scaleMatrix(_object.scaleMatrix) {}
    ~InstanceObject(void) { }

    bool StartUp(const XMFLOAT3 *_position, const XMFLOAT3 *_rotation, const XMFLOAT3 *_scale,
        const XMFLOAT3 *_lookAxis, const XMFLOAT3 *_strafeAxis, const XMFLOAT3 *_upAxis);

    void SetPosition(const XMFLOAT3* _position) { position = XMLoadFloat3(_position); }
    void SetRotationQuaternion(const XMFLOAT3 *_rotation) { rotationQuaternion = XMQuaternionRotationRollPitchYaw(_rotation->x, _rotation->y, _rotation->z); }
    void SetScale(const XMFLOAT3 *_scale) { scale = XMLoadFloat3(_scale); }
}

bool InstanceObject::StartUp(const XMFLOAT3 *_position, const  XMFLOAT3 *_rotation, const XMFLOAT3 *_scale,
        const XMFLOAT3 *_lookAxis, const XMFLOAT3 *_strafeAxis, const XMFLOAT3 *_upAxis) {
    SetPosition(_position);
    SetRotationQuaternion(_rotation);
    SetScale(_scale);   
    SetAnteriorAxis(_lookAxis);
    SetLateralAxis(_strafeAxis);
    SetNormalAxis(_upAxis);

    return true;
}

Any idea what might be causing this behavior and how to fix it?

+5
source share
3 answers

, , XMVECTOR InstanceObject 16 , . - , InstanceObject 0xF , .

, , .

, XMVECTOR ( . - ).

, , , - :

void* InstanceObject::operator new( size_t size )
{
    // _aligned_malloc is a Microsoft specific method (include malloc.h) but its
    // straightforward to implement if you want to be portable by over-allocating
    // and adjusting the address
    void *result = _aligned_malloc( size, 16 );
    if( result )
        return result;

    throw std::bad_alloc(); 
}

void InstanceObject::operator delete( void* p )
{
    if( p ) _aligned_free(p);
}

InstanceObject , _aligned_malloc delete no-op .

+7

! !

// Set component state using the temporary variables.
_pInstanceObject->StartUp(&_position,
    &_rotation,
    &XMFLOAT3(_scale, _scale, _scale), // not valid
    &XMFLOAT3(0.0f, 0.0f, 1.0f), // not valid
    &XMFLOAT3(1.0f, 0.0f, 0.0f), // not valid
    &XMFLOAT3(0.0f, 1.0f, 0.0f) // not valid
    );

. , , .

:

_pInstanceObject->StartUp(_position,
    _rotation,
    XMFLOAT3(_scale, _scale, _scale),
    XMFLOAT3(0.0f, 0.0f, 1.0f),
    XMFLOAT3(1.0f, 0.0f, 0.0f),
    XMFLOAT3(0.0f, 1.0f, 0.0f)
    );


bool StartUp(XMFLOAT3 _position, XMFLOAT3 _rotation, XMFLOAT3 _scale,
    XMFLOAT3 _lookAxis, XMFLOAT3 _strafeAxis, XMFLOAT3 _upAxis);
+4

, XMFLOAT3 , . , .

Try allocating your structures on the heap:

XMFLOAT3* _position = new XMFLOAT3;
0
source

All Articles