I am building C++ interface to be called from C# program (using Dllimport/ dll export). I need to use some pointer during my work. Thus leads me to create an (create_engine) method and (destroy_engine) method. is there a way to use a smart pointers so I do not need the destroy method anymore.
code:
extern "C" _declspec (dllexport) Engine* create_engine(){
return new Engine();
}
extern "C" _declspec (dllexport) void destroy_engine(Engine* engine){
if (engine != NULL){
delete engine;
}
engine = NULL;
}
P.S. the smart pointer will be used from C# so it should stay counting how many references even from the C# part... thanks
No you can't do this. Half manual and half automatic memory management just isn't an option.
The nearest think you can do is to wrap C++ class for use by C# and manage memory there with shared_ptr for example. Example is on MSDN (code bellow also taken from there): Wrap Native Class for Use by C#