C# 强制C+的方法+;类在从C进行互操作调用后被销毁# 我有以下包装器提供C++ C++动态链接库的访问权 #包括“Stdafx.h” #包括“Config.h” #包括“Utils.h” #包括“LogicCompiler.h” 外部“C”{ 库\导出无效的生成项( 常量字符*版本, 常量char*baseDir, 常量字符**文件列表); }//结束外部“C” 无效生成项( 常量字符*版本, 常量char*baseDir, 常量字符**文件列表) { std::字符串标准版本(版本); std::字符串strBaseDir(baseDir); std::vector vFileList=Utils::CharArrayToVector(文件列表); LogicCompiler*编译器=新的LogicCompiler(strVersion、strBaseDir、vFileList); 编译器->m_预处理器->GenerateTables(); }
此调用运行良好,C# 强制C+的方法+;类在从C进行互操作调用后被销毁# 我有以下包装器提供C++ C++动态链接库的访问权 #包括“Stdafx.h” #包括“Config.h” #包括“Utils.h” #包括“LogicCompiler.h” 外部“C”{ 库\导出无效的生成项( 常量字符*版本, 常量char*baseDir, 常量字符**文件列表); }//结束外部“C” 无效生成项( 常量字符*版本, 常量char*baseDir, 常量字符**文件列表) { std::字符串标准版本(版本); std::字符串strBaseDir(baseDir); std::vector vFileList=Utils::CharArrayToVector(文件列表); LogicCompiler*编译器=新的LogicCompiler(strVersion、strBaseDir、vFileList); 编译器->m_预处理器->GenerateTables(); },c#,c++,dll,interop,destructor,C#,C++,Dll,Interop,Destructor,此调用运行良好,GenerateTables完成了它的工作。问题是,LogicCompiler*编译器的析构函数从未被调用。在那里执行一些操作(清理等)。为了处理compiler对象并尝试启动析构函数,我添加了free(compiler)到上面显示的GenerateTables()方法的末尾,但这也不会调用析构函数 如何强制正确地处理编译器对象,从而调用类析构函数 谢谢你抽出时间 编辑。我尝试的第一件事是使用delete编译器但它没有编译。永远不要使用new和free()或malloc()和d
GenerateTables
完成了它的工作。问题是,LogicCompiler*编译器
的析构函数从未被调用。在那里执行一些操作(清理等)。为了处理compiler
对象并尝试启动析构函数,我添加了free(compiler)
到上面显示的GenerateTables()
方法的末尾,但这也不会调用析构函数
如何强制正确地处理编译器对象,从而调用类析构函数
谢谢你抽出时间
编辑。我尝试的第一件事是使用
delete编译器代码>但它没有编译。永远不要使用new
和free()
或malloc()
和delete
,因为这是通往编程地狱的道路
因此,既然您为对象分配了new
,您应该使用delete
释放它。使用free()
< >在C++中,只需要在使用< <代码> >代码> /<代码> For()/<代码>时,应重载<代码>新< /Calp>运算符,分别为<代码>删除>代码>运算符。即使这样,您也应该使用STL对应项(std::malloc()
和std::free()
)当然,您必须使用delete
操作符。或者干脆让编译器用LogicCompiler编译器处理它(…);切勿将new
/delete
与malloc()
/free()
混用,因为它是未定义的behavior@HansPassant,我无法使用delete编译器获取要编译的代码代码>。第二句话是什么意思?delete编译器的编译错误是什么代码>?我不会猜测“未编译”。正确记录你的问题。谢谢你的建议。我尝试的第一件事是使用delete
,但编译器不允许我这么做。。。
#include "Stdafx.h"
#include "Config.h"
#include "Utils.h"
#include "LogicCompiler.h"
extern "C" {
LIBRARY_EXPORT void GenerateTables(
const char* version,
const char* baseDir,
const char** fileList);
} // end extern "C"
void GenerateTables(
const char* version,
const char* baseDir,
const char** fileList)
{
std::string strVersion(version);
std::string strBaseDir(baseDir);
std::vector<std::string> vFileList = Utils::CharArrayToVector(fileList);
LogicCompiler* compiler = new LogicCompiler(strVersion, strBaseDir, vFileList);
compiler->m_PreProcessor->GenerateTables();
}