Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

I have a class (simplified):

class A {
    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.


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
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.


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


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

share|improve this answer

Your Answer


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.