l3utterfly
l3utterfly

Reputation: 2186

Using XNA Math in a DLL Class

I'm having a problem in using XNA Math in a DLL I'm creating. I have a class that is in a DLL and is going to be exported. It has a member variable of type XMVECTOR. In the class constructor, I try to initialize the XMVECTOR. I get a Access Violation in reading from reading location 0x0000000000

The code runs something like this:

class DLLClass 
{
public:
    DLLClass(void);
    ~DLLClass(void);
protected:
    XMVECTOR vect;
    XMMATRIX matr;
}

DLLClass::DLLClass(void)
{
    vect = XMLoadFloat3(&XMFLOAT3(0.0f, 0.0f, 0.0f));    //this is the line causing the access violation
}

Note that this class is in a DLL that is going to be exported. I do not know if this will make a difference by just some further info.

Also while I'm at it, I have another question:

I also get the warning: struct '_XMMATRIX' needs to have dll-interface to be used by clients of class 'DLLClass'

Is this fatal? If not, what does it mean and how can I get rid of it? Note this DLLClass is going to be exported and the "clients" of the DLLClass is probably going to use the variable 'matr'.

Any help would be appreciated.

EDIT: just some further info: I've debugged the code line by line and it seems that the error occurs when the return value of XMLoadFloat3 is assigned to the vect.

Upvotes: 0

Views: 252

Answers (2)

Chuck Walbourn
Chuck Walbourn

Reputation: 41107

This code is only legal if you are building with x64 native -or- if you use __aligned_malloc to ensure the memory for all instances of DLLClass are 16-byte aligned. x86 (32-bit) malloc and new only provide 8-byte alignment by default. You can 'get lucky' but it's not stable.

class DLLClass 
{
public:
   DLLClass(void);
   ~DLLClass(void);
protected:
   XMVECTOR vect;
   XMMATRIX matr;
}

See DirectXMath Programming Guide, Getting Started

You have three choices:

  1. Ensure DLLClass is always 16-byte aligned
  2. Use XMFLOAT4 and XMFLOAT4X4 instead and do explicit load/stores
  3. Use the SimpleMath wrapper types in DirectX Tool Kit instead which handle the loads/stores for you.

Upvotes: 1

japreiss
japreiss

Reputation: 11251

You shouldn't take the address of an anonymous variable:

vect = XMLoadFloat3(&XMFLOAT3(0.0f, 0.0f, 0.0f));

You need

XMFLOAT3 foo(0.0f, 0.0f, 0.0f);
vect = XMLoadFloat3(&foo);

Upvotes: 0

Related Questions