• Vincent Richomme

    I am trying to use Loki::SmartPtr and I
    don't understand how it works :

    I am using it like this :

    // Gdiplus is a namespace holding objects used to draw on Windows
    typedef Loki::SmartPtr<Gdiplus::GraphicsPath> GraphicsPathPtr ;

    typedef std::vector<GraphicsPathPtr> GraphicsPathList;
    typedef std::vector<GraphicsPathPtr>::iterator GraphicsPathListIt;

    GraphicsPathPtr path;

    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)

        path = new GraphicsPath(FillMode::FillModeAlternate);

        return bHandled = FALSE;

    LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
        path = new GraphicsPath(FillMode::FillModeAlternate); // CRASH !!!!
        path->AddBeziers(&allPoints[0], allPoints.size());

        return 0;

    When my application is launched I create a Gdiplus::GraphicsPath, then user
    draw on screen and
    when he releases mouse button OnLButtonUp is called.

    Inside OnLButtonUp  I first want to delete the pointer so path.~SmartPtr is called
    that seem to delete the pointer.
    However when I do path = new GraphicsPath(FillMode::FillModeAlternate); it
    seems smart pointer
    is still using the deallocated pointer and it crashs

    I am sure I am doing something wrong but don't know what...


    • Vincent Richomme

      path = NULL seems to work ...

      • Rick van Haasen

        Rick van Haasen - 2009-04-17

        you shouldn't call the destructor,
        just call
        path = new GraphicsPath(FillMode::FillModeAlternate)

        the smartpointer will do the householding: if it holds the only reference to the pointee, it will delete it before assigning it to the new object.

        path = 0  causes the same effect, but extra step is not needed.

    • Vincent Richomme

      Hum actually not sure it works fine ... because it still crash but later on.
      I think smartpointer are too smart for me.

      • Rick van Haasen

        Rick van Haasen - 2009-04-17

        before reassigning, you could check the reference count of the smrtptr, to see if the refcount is 1 . If not, another smartpointer (one that you store in the array?) still holds a reference to the object.


Log in to post a comment.