Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I believe this is not strictly specific to DirectX but just to pure c++ language problem.

I have a class (simplified):

class A {
public:
    ID3D11Buffer* getVBuffer() const {
        return m_vbuffer;
    }

    // I will make this public for this example
    ID3D11Buffer* m_vbuffer;
};

and then in some other place I am creating vertex buffer:

device->CreateBuffer( ..., &instanceOfA->m_vbuffer );

it works ok, but why with the getter method doesn't?

ID3D11Buffer* _vertexBuffer = instanceOfA->getVBuffer();
device->CreateBuffer( ..., &_vertexBuffer  );

it crashes at Map function with access violation, but I'd like to have getter method used.

// EDIT

when I wrote like this:

device->CreateBuffer( ..., &instanceOfA->getVBuffer()  );

the compiler (VS2012) told me:

error C2102: '&' requires l-value

Is it then even possible to use getters in this situation?

share|improve this question
    
From my experiance with DX, i dont think you should return the buffer as a const. And allso check you error code for CreateBuffer. And what crash dose appear on Map? What does the debug output say? –  Tordin Jan 14 '13 at 9:54
    
Buffer is not returned as a const, const is only the method. HRESULT code is ok (at least it doesn't go into if(FAILED(result)){} statement. I wrote what crash it is - access violation. –  tobi Jan 14 '13 at 10:18
    
Oh, yes, my mistake there. Have you enabled the dx debug? It outputs alot of messages that are helpfull. To enable the debug, go to dx controllpanel, and under dx10/11, there is an edit list. Add your program to that list and enable Force ON. you will now get warnings/errors/info in your output window. see if that could give you a clue. –  Tordin Jan 14 '13 at 10:29
add comment

1 Answer 1

up vote 2 down vote accepted

It's a pointer misunderstanding.

Consider this, even more simplified example:

ID3D11Buffer *original_buffer = NULL;
ID3D11Buffer *returned_buffer = original_buffer;

Okay, so you have two pointers that both point to the same location. This is what happens when you call the getter. The only thing they have in common is that they point to the same address.

device->CreateBuffer(&returned_buffer);

Now, your "returned_buffer" points to the newly created buffer. The "original_buffer" still points to its initial value NULL.

device->MapBuffer(original_buffer);

... and you try to map NULL. Boom!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.