Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

SmartPtr

2009-04-17
2013-04-08
  • 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)
    {
        AtlCreateEmptyMenuBar(m_hWnd);

        ...
        path = new GraphicsPath(FillMode::FillModeAlternate);

        return bHandled = FALSE;
    }

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

        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...

    Regards

     
    • path = NULL seems to work ...

       
      • 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.

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

       
      • 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.