0

I have a class that has a std::shared_ptr as a member, which is later initialised in a function. However, I don't believe it's working correctly as when I debug it and inspect the layout of the shared_ptr object, it shows <Unable to read memory>when I try to view the inner pointer (the class the shared_ptr is pointing to).

The first three file snippets below are from a DLL project that a second project references. The IGLSLProgram.h file is included in this second project so that the exported class defined in the DLL can be used.


IGLSLProgram.h (Interface in the DLL)

class IGLSLProgram;
typedef IGLSLProgram *GLSLProgramHandle;
typedef shared_ptr<IGLSLProgram> IGLSLProgramPtr;
extern "C" COMMON_API GLSLProgramHandle APIENTRY getGLSLProgram();

class IGLSLProgram {
public:
    IGLSLProgram() {}
    ~IGLSLProgram() {}

    // ...

    virtual void release() = 0;
};

GLSLProgram.h (Implements IGLSLProgram)

#include "IGLSLProgram.h"

class GLSLProgram : public IGLSLProgram {
public:
    GLSLProgram() {}
    ~GLSLProgram() {}

    // ...

    void release();
};

GLSLProgram.cpp (Defines GLSLProgram implementation)

#include "GLSLProgram.h"

COMMON_API GLSLProgramHandle APIENTRY getGLSLProgram() {
    return new GLSLProgram;
}

// ...

Program.h (Program that uses the DLL)

#include "Common\IProgram.h"
#include "Common\IGLSLProgram.h"

class Program : public IProgram {
protected:
    IGLSLProgramPtr shaderProgram;
public:
    Program() {}
    ~program() {}

    void createShaderProgram();

    // ...
};

Program.cpp (Defines class declared in Program.h)

#include "Program.h"
#include "Common\IGLSLProgram.h"

// ...

void Program::createShaderProgram() {
    shaderProgram = IGLSLProgramPtr(getGLSLProgram(), mem_fn(&IGLSLProgram::release));

    // ...

}


When inspecting the last line in the Program.cpp snippet, I see that shaderProgram has the following layout: http://i.stack.imgur.com/VZrkF.jpg

Any help with this issue would be greatly appreciated.

4

1 回答 1

0

我通过IGLSLProgram shaderProgram;Program.h中设置静态来解决了这个问题。这对我的目的来说很好,因为 Program 类应该只有一个实例。


Program.h(已解决)

#include "Common\IProgram.h"
#include "Common\IGLSLProgram.h"

class Program : public IProgram {
protected:
    static IGLSLProgramPtr shaderProgram;
public:
    Program() {}
    ~program() {}

    void createShaderProgram();

    // ...
};

我不确定的是为什么这会有所帮助以及为什么 astd::shared_ptr不能成为类中的非静态成员。如果有人能解释这一点,我将不胜感激。

于 2013-08-10T11:26:15.697 回答