0

I have a fairly large legacy application, which uses only normal pointers. Right now I am adding a new module, trying to use "more modern stuff", e.g. smart pointers.

The main module sends a message over a communication DLL to my new submodule (with a normal pointer). In my new submodule I am using shared_pointer for message handling. When the shared pointer tries to delete the pointer, I get an access violation in the dectructor of the message, because VTable pointer is 0xdddddddd. I have already found this SO question , which says this is because the pointer has already been released.

It seems the main module is already deleting the normal pointer, before shared pointer deletes it.

I don't want to use shared pointers in the old main module (would be very much refactoring at this point), but is there a solution how I can use both normal and shared pointers in my application? The usual NULL check in the destructor does not help.

Community
  • 1
  • 1
Simon
  • 1,616
  • 2
  • 17
  • 39

1 Answers1

1

You need to make sure that the main module transfers the ownership of the pointer.
i.e: It should not maintain any pointer to that address after it is sent to your module. Otherwise it may deallocate the pointer leaving your module with a dangling pointer.

So, If the main module maintains references to the pointer then using shard_ptr just for your module doesn't make sense. In that case you will need to use shared_ptr in the main module too.

Also, you need to make sure that you are using appropriate memory deallocation routine for the pointer in the shared_ptr::deleter().
It might be the case that there is mis-match in allocation and deallocation routines being used for the pointer.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • This helped me a lot understanding what to do when i improve our legacy app. However, I narrowed the problem down and it seems there is a problem in my submodule – Simon Jan 27 '12 at 11:41
  • @Simon: No problem.Wish you luck :) – Alok Save Jan 27 '12 at 15:40