views:

201

answers:

4

Hi,

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

+9  A: 

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!

sdg
Looking at the wealth of code I'm starting to think you're right. Thank you for stopping me going on a crazy code cull.
Rich
+5  A: 

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.

Alexandre Jasmin
+1: it is important the remark about cyclic references. That is something that must be taken care of if using smart pointers (using `weak_ptr` to break cycles)
David Rodríguez - dribeas
An important point about the Boehm collecter is that it's conservative -- it's not guaranteed to clean up all garbage. Nor is it deterministic, like all non-reference counted collectors.
Joel
+3  A: 

There is no easy way to do this. As you've found out, boost::shared_ptrs and standard pointers are not interchangeable. What you are doing here is refactoring code, and unfortunately refactoring takes a long time and can be very tedious.

As sdg said, typedefing pointers for shared_ptrs is not a good idea, and just increases the amount of code you have to write.

First, I would identify the pointers that actually need to be changed to shared_ptrs. Obviously you don't want to be changing all pointers to shared_ptrs. Most would probably be better off as std::auto_ptrs or boost::scoped_ptrs and some would be better as boost::weak_ptr, and finally some might just be fine as plain C-style pointers.

Just go through each pointer that needs changing one-by-one, find all references to it, and make the necessary adjustments (e.g. removing calls to delete).

Peter Alexander
A: 

I would be very restrictive in introducing shared_ptr into an existing large codebase. If you really want to use smart-pointers to fix errors I suggest using scoped pointers and other than that I would refactor code and design clear ownership.

Simon