class 方法中向量的 push_back 异常暴露给 DLL
Exception on push_back to vector in method of class exposed to DLL
我创建了一个 DLL 库,然后将其链接到不同的项目中,使用不同的 VS 编译。
只有一个简单的 class 暴露给具有 __declspec(dllexport)
的 DLL,如下所示:
class TFLIBRARY_API MyClass
此 class 的字段之一是 std::vector<std::string>
,此 class 的部分初始化是用数据填充该向量:
std::string model(parsedLine[0]);
m_blocksModels.push_back(model);
这导致我无法理解的异常:
Exception thrown: read access violation.
我附上调用堆栈的屏幕。
这是否意味着 std::vector
不能在图书馆中这样使用?那我有什么选择?
或者,也许存在其他问题?请帮我找一下。
编辑:
按照建议,我附上了一个最小的例子:
Class 本身:
#ifdef LIBRARY_EXPORTS
#define LIBRARY_API __declspec(dllexport)
#endif // LIBRARY_EXPORTS
class LIBRARY_API MyClass
#ifdef LIBRARY_EXPORTS
// to hide in .h file some fields, that have undefined class (from perspective of usage of DLL)
std::unique_ptr<Model> m_model;
std::vector<std::string> m_blocksModels;
#endif // LIBRARY_EXPORTS
public:
bool Initialize(int& numberOfFrames, int& numberOfBlocks, const char* cgPath = "model.pb");
}
初始化定义:
bool MyClass::Initialize(int& numberOfFrames, int& numberOfBlocks, const char* modelPath )
{
std::string cgPath{ modelPath };
cgPath = cgPath.substr(0, cgPath.find_last_of('.')) + ".cfg";
std::ifstream file(cgPath);
if (!file.good())
return false;
std::string line;
// First line is: <number of frames>;<number of blocks>
std::getline(file, line);
std::vector<std::string> parsedLine = utils::parseLine(line);
m_numberOfFrames = std::stoi(parsedLine[0]);
m_numberOfBlocks = std::stoi(parsedLine[1]);;
while (std::getline(file, line)) {
parsedLine = utils::parseLine(line);
std::string model(parsedLine[0]);
m_blocksModels.push_back(model);
}
file.close();
return true;
}
用法:
#include "TFLibrary.h"
obj = MyClass()
obj.Initialize()
所以我不直接使用作为vectr的字段,这一切都发生在Initialize函数中
那是我的代码中内存管理的普遍失败。
我创建了一个 DLL 库,然后将其链接到不同的项目中,使用不同的 VS 编译。
只有一个简单的 class 暴露给具有 __declspec(dllexport)
的 DLL,如下所示:
class TFLIBRARY_API MyClass
此 class 的字段之一是 std::vector<std::string>
,此 class 的部分初始化是用数据填充该向量:
std::string model(parsedLine[0]);
m_blocksModels.push_back(model);
这导致我无法理解的异常:
Exception thrown: read access violation.
我附上调用堆栈的屏幕。
这是否意味着 std::vector
不能在图书馆中这样使用?那我有什么选择?
或者,也许存在其他问题?请帮我找一下。
编辑: 按照建议,我附上了一个最小的例子: Class 本身:
#ifdef LIBRARY_EXPORTS
#define LIBRARY_API __declspec(dllexport)
#endif // LIBRARY_EXPORTS
class LIBRARY_API MyClass
#ifdef LIBRARY_EXPORTS
// to hide in .h file some fields, that have undefined class (from perspective of usage of DLL)
std::unique_ptr<Model> m_model;
std::vector<std::string> m_blocksModels;
#endif // LIBRARY_EXPORTS
public:
bool Initialize(int& numberOfFrames, int& numberOfBlocks, const char* cgPath = "model.pb");
}
初始化定义:
bool MyClass::Initialize(int& numberOfFrames, int& numberOfBlocks, const char* modelPath )
{
std::string cgPath{ modelPath };
cgPath = cgPath.substr(0, cgPath.find_last_of('.')) + ".cfg";
std::ifstream file(cgPath);
if (!file.good())
return false;
std::string line;
// First line is: <number of frames>;<number of blocks>
std::getline(file, line);
std::vector<std::string> parsedLine = utils::parseLine(line);
m_numberOfFrames = std::stoi(parsedLine[0]);
m_numberOfBlocks = std::stoi(parsedLine[1]);;
while (std::getline(file, line)) {
parsedLine = utils::parseLine(line);
std::string model(parsedLine[0]);
m_blocksModels.push_back(model);
}
file.close();
return true;
}
用法:
#include "TFLibrary.h"
obj = MyClass()
obj.Initialize()
所以我不直接使用作为vectr的字段,这一切都发生在Initialize函数中
那是我的代码中内存管理的普遍失败。