我有一个以std::shared_ptr
为成员的类,稍后在函数中初始化。但是,我不相信它正常工作,因为当我调试它并检查shared_ptr对象的布局时,当我尝试查看内部指针(<Unable to read memory>
指向的类时,它显示shared_ptr
到)。
下面的前三个文件片段来自第二个项目引用的DLL项目。 IGLSLProgram.h文件包含在第二个项目中,以便可以使用DLL中定义的导出类。
IGLSLProgram.h(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(实施IGLSLProgram)
#include "IGLSLProgram.h"
class GLSLProgram : public IGLSLProgram {
public:
GLSLProgram() {}
~GLSLProgram() {}
// ...
void release();
};
GLSLProgram.cpp(定义GLSLProgram实现)
#include "GLSLProgram.h"
COMMON_API GLSLProgramHandle APIENTRY getGLSLProgram() {
return new GLSLProgram;
}
// ...
Program.h(使用DLL的程序)
#include "Common\IProgram.h"
#include "Common\IGLSLProgram.h"
class Program : public IProgram {
protected:
IGLSLProgramPtr shaderProgram;
public:
Program() {}
~program() {}
void createShaderProgram();
// ...
};
Program.cpp(定义在Program.h中声明的类)
#include "Program.h"
#include "Common\IGLSLProgram.h"
// ...
void Program::createShaderProgram() {
shaderProgram = IGLSLProgramPtr(getGLSLProgram(), mem_fn(&IGLSLProgram::release));
// ...
}
检查Program.cpp片段中的最后一行时,我发现shaderProgram具有以下布局:http://i.stack.imgur.com/VZrkF.jpg
非常感谢任何有关此问题的帮助。
答案 0 :(得分:0)
我在 Program.h 静态中使IGLSLProgram shaderProgram;
解决了这个问题。这对我来说很好,因为Program类应该只有一个实例。
Program.h(已解决)
#include "Common\IProgram.h"
#include "Common\IGLSLProgram.h"
class Program : public IProgram {
protected:
static IGLSLProgramPtr shaderProgram;
public:
Program() {}
~program() {}
void createShaderProgram();
// ...
};
我不确定这是为什么这有助于为什么std::shared_ptr
不能成为类中的非静态成员。如果有人能解释清楚,我会非常感激。