是否可以从 dll 中删除在应用程序中创建的对象
Is it okay to delete an object that is created in the app from dll
我有一个带有 class 的 dll,它使用抽象 class 来自定义行为,它还有一个在 dll
中定义的实现
有了这个,应用程序分配了一个 Child 对象并将其传递到 class A
中,当它被删除时它会释放该对象
从 dll 中删除在应用程序中创建的对象是否会产生问题
如果它有任何修复它的想法
代码大致翻译成这样
// Dll
DLLEXPORT class A
{
private:
Base* ptr;
public:
A(Base* ptr) { this->ptr = ptr; };
~A() { delete ptr; }
};
DLLEXPORT class Base
{
virtual int foo() = 0;
};
DLLEXPORT class Child : public Base
{
virtual int foo() { return 1; }
};
// App
int main()
{
A obj(new Child);
return 0;
}
您可以遵循 Cpp Core Guidelines 约定来指定资源所有权的转移,以明确会发生什么。
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-owner
其中还有很多其他好的指南,比如使用初始化而不是成员变量赋值。
MS 是这样说的:
The DLL allocates memory from the virtual address space of the calling process (docs)
在这个回答中你可以看到:
If your DLL allocates memory using C++ functions, it will do so by calling operator new in the C++ runtime DLL. That memory must be returned by calling operator delete in the (same) C++ runtime DLL. Again, it doesn't matter who does that.
如果 DLL
、c++
运行时 and/or 应用程序是使用不同的编译器编译的,您可能会遇到麻烦;或者如果其中一些是静态编译的。
为避免这些问题,您可以将“删除器”object/function 传递给 DLL
,以便它在应用程序中进行删除:
DLL:
class Base;
typedef void (*deleter_t)(Base * ptr);
deleter_t app_deleter {nullptr};
DLLEXPORT class A
{
private:
Base* ptr;
public:
A(Base* ptr) { this->ptr = ptr; };
~A() { app_deleter(ptr); }
};
DLLEXPORT void set_deleter (deleter_t func)
{
app_deleter = func;
}
应用程序:
void deleter (Base * obj)
{
delete obj;
}
int main()
{
set_deleter (deleter);
A obj(new Child);
return 0;
}
详情请见:SO answer
我有一个带有 class 的 dll,它使用抽象 class 来自定义行为,它还有一个在 dll
中定义的实现有了这个,应用程序分配了一个 Child 对象并将其传递到 class A
中,当它被删除时它会释放该对象
从 dll 中删除在应用程序中创建的对象是否会产生问题 如果它有任何修复它的想法
代码大致翻译成这样
// Dll
DLLEXPORT class A
{
private:
Base* ptr;
public:
A(Base* ptr) { this->ptr = ptr; };
~A() { delete ptr; }
};
DLLEXPORT class Base
{
virtual int foo() = 0;
};
DLLEXPORT class Child : public Base
{
virtual int foo() { return 1; }
};
// App
int main()
{
A obj(new Child);
return 0;
}
您可以遵循 Cpp Core Guidelines 约定来指定资源所有权的转移,以明确会发生什么。
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-owner
其中还有很多其他好的指南,比如使用初始化而不是成员变量赋值。
MS 是这样说的:
The DLL allocates memory from the virtual address space of the calling process (docs)
在这个回答中你可以看到:
If your DLL allocates memory using C++ functions, it will do so by calling operator new in the C++ runtime DLL. That memory must be returned by calling operator delete in the (same) C++ runtime DLL. Again, it doesn't matter who does that.
如果 DLL
、c++
运行时 and/or 应用程序是使用不同的编译器编译的,您可能会遇到麻烦;或者如果其中一些是静态编译的。
为避免这些问题,您可以将“删除器”object/function 传递给 DLL
,以便它在应用程序中进行删除:
DLL:
class Base;
typedef void (*deleter_t)(Base * ptr);
deleter_t app_deleter {nullptr};
DLLEXPORT class A
{
private:
Base* ptr;
public:
A(Base* ptr) { this->ptr = ptr; };
~A() { app_deleter(ptr); }
};
DLLEXPORT void set_deleter (deleter_t func)
{
app_deleter = func;
}
应用程序:
void deleter (Base * obj)
{
delete obj;
}
int main()
{
set_deleter (deleter);
A obj(new Child);
return 0;
}
详情请见:SO answer