I am adding a piece of code, all that I am doing in this is creating an object of class csocket, and adding into the vector. Then erase the object and add again. but when I check the address of a member variable pointer _pImpl its always the same.
I don't know weather this is system behavior or vector. Can some body explain this behavior
#include <iostream>
#include <vector>
usingnamespace std;
typedefstruct csock {
unsignedint _refCount;
mutable pthread_mutex_t _pdata_mutex;
pthread_attr_t attr;
bool _bSocketClosed;
int _iSockFD;
void* _pfnArgs;
void* _logger;
bool _isServiceRunning;
pthread_t _thread_id;
int _pipes[2];
pthread_mutex_t* _countMutex;
unsignedlong _idleTimeOut;
FILE* fp;
} __attribute__((packed)) csock_t;
class csocket
{
protected:
void* _pImpl;
public :
csocket(){
csock_t* ps = new csock_t;
this->_pImpl = reinterpret_cast<void*> (ps);
std::cout<<"\n ps is "<<ps <<" _pImpl is "<<_pImpl <<endl;
}
void* getImpl()
{
return _pImpl;
}
};
int main ()
{
vector<csocket> v;
for (int i=0; i< 5; ++i) {
v.push_back(csocket());
cout<<"\n after pushback "<<v[v.size()-1].getImpl();
delete (csock_t*) v[0].getImpl();
v.erase(v.begin()+0);
}
cout <<"\n size of vector is "<<v.size();
return 0;
}
Here is the out put in my system
ps is 0x8f73008 _pImpl is 0x8f73008
after pushback 0x8f73008
ps is 0x8f73008 _pImpl is 0x8f73008
after pushback 0x8f73008
ps is 0x8f73008 _pImpl is 0x8f73008
after pushback 0x8f73008
ps is 0x8f73008 _pImpl is 0x8f73008
after pushback 0x8f73008
ps is 0x8f73008 _pImpl is 0x8f73008
after pushback 0x8f73008
I don't see how this is due to vector. You do new, delete, and then new again, it is very likely, that memory will be allocated at the same spot. Why wouldn't it..
Sorry I realized it after posting the query that it has nothing to do with vector. So it is possible to get the same memory chunk back as soon as it is deleted.
I am surprised that they are the same with that use of cout in the loop.
You create a csocket, that creates a csock_t and you add it to the vector. Then you delete the csock_t, then remove the element from the vector which in turn deletes the csocket. Then you repeat five times.
I notice you've not posted the final line: size of vector is 0
That should be a hint on what the vector is doing. The vector capacity doen't change (it doesn't give back the memory) even though its size changes (between 0 and 1).
my bad ! I missed it. Actually i have a multi threaded application where the deletion and removing from vector happens in different threads, also they may not be synchronous. I was actually removing it from vector on the basis of the address of the _pImpl and this pointer. But it lead me to serious crashes. Then I realized that the values in the vector are identical, so I posted this.
I think I need to use boost shared pointers rather than doing the reference count myself.
I found the reason for the crash.I was actually doing some thing like this.
step 1. create and add a csocket object to vector.
step 2. Then I call some member function of csocket by vector[0].startthread() .
step 3. the start thread starts a pthread and passes the "this" pointer as argument to the thread function.
step 4. this happens every time a client connects to my server.But when a client disconnects I remove that csocket object from the vector. This results in a reshuffle of vector elements.
step5. After this all the *this" becomes invalid because vector reshuffle gives a new address toall the elements after reshuffling, making *this invalid.