jeudi 17 février 2011

c++ deletion of an incomplete type

Today I had an issue when I implemented a smart_ptr for my dblite project.
I wanted to move out the dependency for some reason (iphone,ipad).

In this library I am using the pimpl idiom and once I replaced the shared_ptr of boost by my version of it, I faced a warning of the compiler: 

warning: possible problem detected in invocation of delete operator:

Hopefully I found the solution to my problem here:
Private implementation using smart pointers and deletion trouble

I didn't understand clearly what was the workaround used by boost::shared_ptr to work on incomplete type, but it was surely useful. As far as I understood, the trick would be to instantiate an deleter object when the pointer is assigned so at this moment the type is complete.

ps: I found the trick!
using a function pointer to delete.
It is very well explained here:

2 commentaires:

  1. I had this same issue too. The Function Pointer trick seemed to work. But, I found something else. Does the class that contains your smart_ptr have a destructor in your header file? Mine did, and that was the problem. The destructor was defined in the header file, but I only had forward declarations in the header file. Once I moved my destructor definition in the source file (.cc), I didn't have this problem anymore.

    1. I discovered afterwards that the warning disappears if you declare the destructor of the private class before the destructor of the public class.