boost::shared_ptr with std::vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: boost::shared_ptr with std::vector

  1. #1
    Join Date
    Dec 2010
    Posts
    523

    boost::shared_ptr with std::vector

    Code:
    std::vector<CObjects*> m_pRenderObjects;
    
    //////// Load Warehouse info
    		 
    		boost::shared_ptr<CWarehouse> sobj(new CWarehouse());
    		m_Warehouse.Load(m_pd3dDevice, _effect, _T("Data\\DemoWarehouse.x"));
    
    		sobj->Create(&m_Warehouse);
    	 
    
    		/////// Load CB info positions, models etc
    		 
    		boost::shared_ptr<CCB> sccb(new CCB());
    		m_CCB.Load(m_pd3dDevice, _effect, _T("Data\\cb.x"));
    
    		sccb->Create(&m_CCB);
    
    
    
    
    		m_pRenderObjects.push_back(sobj.get());
    
    		m_pRenderObjects.push_back(sccb.get());
    
    
    //////////////////////
    
    for (int i = 0; i < objects.size(); i++)
        objects[i]->Render();
    error:
    0xC0000005: reading location 0xfeeefef2 [Translated]
    Last edited by lucky6969b; August 12th, 2012 at 03:17 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,418

    Re: boost::shared_ptr with std::vector

    Quote Originally Posted by lucky6969b View Post
    Code:
    std::vector<CObjects*> objects;
    
    
    boost::shared_ptr<CWarehouse> wh(new CWarehouse());
    
    objects.push_back(wh.get());
    
    
    for (int i = 0; i < objects.size(); i++)
        objects[i]->Render();
    error:
    0xC0000005: reading location 0xfeeefef2 [Translated]
    The shared pointer is a red herring. All you're doing is placing regular pointers (actually the same pointer value) inside the vector and calling Render(). Also, is this your entire code?

    It is no different than this:
    Code:
    std::vector<CObjects*> objects;
    CWarehouse* wh = new CWarehouse();
    objects.push_back(wh);
    for (int i = 0; i < objects.size(); i++)
        objects[i]->Render();
    So is it the Render() function that is causing the issue? If so, what is Render()? What does it do? Where is the code?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2010
    Posts
    523

    Re: boost::shared_ptr with std::vector

    Code Snippet updated, thanks Paul... I create 2 separate shared_ptrs for each object
    I've got to tell you that why I use shared_ptrs, because I have a direct3d app, I pass m_pd3dDevice of CD3DApplication
    around to other objects, in these objects, I have to build hierarchies, sometimes I have to addref and releaseCOM to these pointers. But at the end of the application, i get m_pd3dDevice not completed cleaned up (non-zero reference counting problem). That's why.
    Last edited by lucky6969b; August 12th, 2012 at 03:29 AM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,418

    Re: boost::shared_ptr with std::vector

    Quote Originally Posted by lucky6969b View Post
    Code Snippet updated, thanks Paul... I create 2 separate shared_ptrs for each object
    Your new code doesn't change anything I stated. The boost shared pointer usage is a red herring and you're being led down the wrong path in fixing the problem. The issue is somewhere else.

    Get rid of the shared pointer, and you should get the same issue.
    Code:
    std::vector<CObjects*> m_pRenderObjects;
    CWarehouse* sobj = new CWarehouse();
    m_Warehouse.Load(m_pd3dDevice, _effect, _T("Data\\DemoWarehouse.x"));
    sobj->Create(&m_Warehouse);
    CCB * sccb = new CCB();
    m_CCB.Load(m_pd3dDevice, _effect, _T("Data\\cb.x"));
    sccb->Create(&m_CCB);
    m_pRenderObjects.push_back(sobj);
    m_pRenderObjects.push_back(sccb);
    for (int i = 0; i < objects.size(); i++)
        objects[i]->Render();
    Same code, no shared pointer. Does this give an error?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2010
    Posts
    523

    Re: boost::shared_ptr with std::vector

    No, it doesn't. But I have m_pd3dDevice not cleaned up problem.

  6. #6
    Join Date
    Dec 2010
    Posts
    523

    Re: boost::shared_ptr with std::vector

    Code:
    //-----------------------------------------------------------------------------
    // Name: Cleanup3DEnvironment()
    // Desc: Cleanup scene objects
    //-----------------------------------------------------------------------------
    void CD3DApplication::Cleanup3DEnvironment()
    {
        if( m_pd3dDevice != NULL )
        {
            if( m_bDeviceObjectsRestored )
            {
                m_bDeviceObjectsRestored = false;
                InvalidateDeviceObjects();
            }
            if( m_bDeviceObjectsInited )
            {
                m_bDeviceObjectsInited = false;
                DeleteDeviceObjects();
            }
    
    		//char buff[10];
    		
    		//OutputDebugString(buff, 
    
            if( m_pd3dDevice->Release() > 0 )
                DisplayErrorMsg( D3DAPPERR_NONZEROREFCOUNT, MSGERR_APPMUSTEXIT );  <<<<<<<<<<<<<<<<<<<< Crashes here
            m_pd3dDevice = NULL;
        }
    }
    Are there anyways I can take snapshot of the reference count by myself?

  7. #7
    Join Date
    Apr 1999
    Posts
    27,418

    Re: boost::shared_ptr with std::vector

    You're asking to debug code we have no idea about. What is this new function you've now introduced that you say crashes?

    But why are you posting different code now, just out of left field like that? What does this have to do with what you posted earlier?

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 1999
    Posts
    27,418

    Re: boost::shared_ptr with std::vector

    But in general, if a program crashes, there is no one here that can help you unless we have the entire program and run it ourselves.

    I really don't get why so many posters believe we have extraordinary powers to debug programs where we have little to no source code, we don't know what data or values are used to run the program, we don't know how the program flows, but are supposed to figure out why a program crashes.

    Maybe the problem starts way before that line where you say crashes. Maybe the entire object is invalid (seeing that it is a member function of a class). All we have are maybe's, unless we have the code, run it, and duplicate the error. Otherwise, program crashes can only be solved by the persons with the program in front of them, running it and duplicating the problem.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Dec 2010
    Posts
    523

    Re: boost::shared_ptr with std::vector

    I can post the entire program to you. As long as you have to time to take a look. I am unsure how to reproduce the error of the bug.
    Thanks

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,632

    Re: boost::shared_ptr with std::vector

    Quote Originally Posted by lucky6969b View Post
    error:
    0xC0000005: reading location 0xfeeefef2 [Translated]
    That type of values usually indicates something. Here's a reference to the values used by the CRT debug runtime http://www.nobugs.org/developer/win3...eap.html#table
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  11. #11
    Join Date
    Jan 2001
    Posts
    251

    Re: boost::shared_ptr with std::vector

    Looking at your first post, I can see a problem with your use of shared_ptr.

    A shared_ptr will delete the object once the last shared_ptr that points to an object goes out of scope. Since you are not storing the shared_ptr in m_pRenderObjects, the variables sobj and sccb will delete the pointer that they hold when they leave scope. Depending on the scope of these objects, this can occur before the loop where you call objects[i]->Render() - for instance if you initialize these in a routine.

    Why is m_pRenderObjects a vector of simple pointers to CObjects? By changing this to a vector containing shared_ptr<CObjects>, the shared_ptr can then keep alive when you add it to the vector. If you can't change m_pRenderObjects, then you shouldn't be using shared_ptr for these objects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center