Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to introduce boost::shared_ptr into an existing (large) C++ codebase?

I am currently trying to fix a few weaknesses in our code base by introducing the use of smart pointers. The code base is very large, and interlinked like a spider who's had one to many coffee's.

I was wondering if people had tried the before and what their approach was.

My first step has been to typedef classes, as follows.

#ifndef USE_SMART_POINTERS
    #define USE_SMART_POINTERS 0
#endif

#if USE_SMART_POINTERS == 1
    #include <boost/smart_ptr.hpp>
#endif


namespace ProductX
{
    // forward decleration
    class CTObject;


    //typedefs
    #if USE_SMART_POINTERS == 1
        typedef boost::shared_ptr<CTObject> CTObjectPtr;
    #else
        typedef CTObject* CObjectPtr;
    #endif
}

Now I realise this will lead to a wealth of compile areas, things like

CTObjectPtr i = NULL;

Will completly bork when smart pointers are enabled.

I was wondering if there was anything I could do at this early stage to reduce the mass of compile errors, or is it as I suspect just take things on a case by case basis.

Cheers Rich

like image 208
Rich Avatar asked Feb 24 '10 16:02

Rich


2 Answers

Don't do this: the typedefs I mean.

Presumably the old code has at least some delete calls in it? Which would certainly fail in the case of a smart pointer.

Smart pointer certain things or not, i.e. chase a specific instance through the code base. Make it work, then move on. Good Luck!

like image 153
sdg Avatar answered Nov 04 '22 20:11

sdg


Instead of trying to introduce smart pointers everywhere you could use the Boehm-Demers-Weiser garbage collector and leave your code base intact.

It will also take care of cyclic references.

like image 5
Alex Jasmin Avatar answered Nov 04 '22 19:11

Alex Jasmin