Search code examples
c++pointersmemory-managementpass-by-referenceshared-ptr

Are pointers to memory copied during data transfer to/from function return values/arguments and class requests?


Let's say I have the following function (where mVertexShader is an std::shared_ptr< ID3D11VertexShader > class member, and mRenderData is just POD holding other D3D stuff):

void VertexShader::CreateShader( void )
{
    GVertexShader* raw_VertexShader = mVertexShader.get();

    mRenderData->Device->CreateVertexShader(
        mCompiledShader->GetBufferPointer(),
        mCompiledShader->GetBufferSize(),
        NULL,
        &raw_VertexShader
    );

    delete raw_VertexShader;
    raw_VertexShader = NULL;
} 

since mVertexShader.get() returns the raw pointer, will deleting raw_VertexShader effect the pointer inside mVertexShader.get(), or, while the memory being pointed to is the same, the pointers themselves are completely different?

I'm guessing the latter, hence why reference-to-pointer exists in the first place, but I'd like to be sure that I have the right idea.

Note: I am currently using MSVC++, though it would be nice to hear whether or not there are any significant differences in how MSVC++ and MinGW/GCC implement this.

Note2: I apologize if I wasn't being clear - I'm not referring to semantics of D3D here at all, but more so the semantics of the language of C++ itself in regards to memory management, data transfer, and dynamic memory allocation. In that sense, what I'm trying to understand exactly what happens when a raw pointer is returned from a function.

I.e., assuming mVertexShader is storing a simple raw pointer to an arbitrary VertexShader, when I call its get() method, does it return a copied pointer which points to the same memory address being referenced as the one which sits inside the shared_ptr container, or is it an entirely new pointer altogether that has been copied and returned, with memory being to pointed to at a different address?


Solution

  • This is actually entirely about the semantics of std::shared_ptr. The language is perfectly happy to support methods of returning a pointer to an object, making a copy of an object and returning a pointer to that, or bumping the reference count on an object and returning a pointer to it. You can do all of those things if you want to.

    The get method on a shared pointer just returns the raw pointer to the object. No copy is made. You should never delete this pointer for two reasons:

    1. Another object might have a shared pointer to the same object and you'd be deleting the object out from under it. (That's the point of shared pointers.)

    2. When the last shared pointer goes away, the object will be deleted. Deleting an object twice is UB and can cause your code to crash. (That's also the point of shared pointers.)

    Let the shared pointers do their job.

    Note that if you call get on a shared pointer, you should make sure you keep that shared pointer around for as long as you might use the raw pointer that you got. Otherwise, the object may cease to exist. The logic of shared pointers is that as long as you keep at least one shared pointer around that references an object, it will not be deleted and when the last such shared pointer goes away, the object deletes automatically.