1

ALL,

This is a continuation of this thread.

It looks like that the pointer that has been allocated inside DLL will become bad after the DLL is unloaded. Following suggestion by TBBle given in the mentioned thread it works if I immediately delete the pointer in the same function, but if I return the pointer back to the main application and then will try to access it from a different function, the pointer becomes bad.

I believe that even if I put the Foo class inside the main application it will not do me any good but I may be wrong. The reason for that is because the DLL2 will be unloaded with the unloading of DLL3.

So any idea how to make the compiler happy, don't introduce any memory leaks and don't crash?

Thank you.

P.S.: I am just follow the suggestion given at the end of the comments to create a new question. Apologies if this is bad thing to do.

Community
  • 1
  • 1
Igor
  • 4,364
  • 8
  • 38
  • 80
  • Pointer ownership has to have some coherence. Who owns the pointer? You shouldn't just allocate memory and have it as a free-for-all as to who manages the lifetime of the pointer. If it is that unruly, then consider a `std::shared_ptr`. – PaulMcKenzie Feb 06 '16 at 23:21
  • @PaulMcKenzie, pointer management is done in the main application. the owner is main application window. The main application window class has a member of class Foo which is assigned the pointer address after the function return. Trouble is there the pointer is good. But when I close the main window it becomes bad. Nobody touches the pointer in the IDLE time and destructor is not called. – Igor Feb 06 '16 at 23:40
  • 3
    Pimpl is too weak to allow you to build your DLL with /MT. It is imperative that it is your DLL that destroys the object, you can't allow the client code to do it since it is using its own copy of the C runtime library. In other words, you must expose a "destroy" function and not allow the client code to invoke your destructor. An interface is the proper way, reference counting is the standard approach to this kind of memory management. Compare to the IUnknown interface in COM, just three functions to do everything. – Hans Passant Feb 06 '16 at 23:46
  • @HansPassant, I am recompiling everything with /MDd. Will post here after words. About "pimpl" - I used to get rid of the "std::vector<> should be exported" warning. Also, DLL should not destroy the object - main application will. So I am going to try /MDd and hopefully it will fix it. – Igor Feb 06 '16 at 23:52
  • @PaulMcKenzie, std::shared_ptr is C++11 only or not? – Igor Feb 06 '16 at 23:54
  • @HansPassant, after I rebuilt everything with /MDd there is no more crash. Thank you. – Igor Feb 07 '16 at 01:31
  • @HansPassant, apparently I was too fast again. :( Everything worked until I move the code deleting the pointer to the main frame destructor. Then the code started crashing again. I can successfully pass the pointer to another function back and force but deleting the pointer crashes. Any idea? – Igor Feb 08 '16 at 04:48

0 Answers0