I have a class, say Container which contains objects of another class, for example MyObject. MyObject does some processing, and in some future time it is no longer needed. Hence, it should somehow tell to the Container about it. I currently do something like this:
I could create a thread in the Container class and poll the objects, which can provide something like: bool DoneProcessing( ) and then can be deleted, but i believe it may be a worse solution.
So, I wonder if it's safe to do the things the way i do: deleting the object in the callback function that it called, or maybe there is a better solution that i haven't thought of...
Thanks in advance
Code:
class MyObject { public: typedef void(*PFN)(void*,MyObject*); MyObject(void* param, PFN p) : m_param(param), m_pfn(p) {} // some other functions private: void Done() { m_pfn(m_param, this); } void* m_param; PFN m_pfn; }; class Container { public: Container() { // ... // just an example for (int i = 0; i != 100; ++i) { MyObject* pobj = new MyObject(this, MyObjectCallback); m_Vector.push_back(pobj); } } // other functions... static DWORD WINAPI MyObjectCallback(void* p, MyObject* pobj) { Container *pThis = (Container*)p; pThis->lockVector(); vector<MyObject*>::iterator it = pThis->m_Vector.begin(); while (it != pThis->m_Vector.end()) { if (*it == pobj) { delete *it; pThis->m_Vector.erase(it); break; } else ++it; } pThis->unlockVector(); } private: vector<MyObject*> m_Vector; };
I could create a thread in the Container class and poll the objects, which can provide something like: bool DoneProcessing( ) and then can be deleted, but i believe it may be a worse solution.
So, I wonder if it's safe to do the things the way i do: deleting the object in the callback function that it called, or maybe there is a better solution that i haven't thought of...
Thanks in advance
Comment