[英]Unable to read memory while using std::shared_ptr
我有一個具有std::shared_ptr
作為成員的類,該類稍后在函數中初始化。 但是,我不認為它在調試和檢查shared_ptr對象的布局時能正常工作,當我嘗試查看內部指針( shared_ptr
指向的類)時,它顯示<Unable to read memory>
。
下面的前三個文件片段來自第二個項目引用的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
任何與此問題的幫助將不勝感激。
我通過制作IGLSLProgram shaderProgram;
解決了這個問題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();
// ...
};
我不確定為什么這會有所幫助,以及為什么std::shared_ptr
不能是類中的非靜態成員。 如果有人能解釋這一點,我將非常感謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.