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