I have a problem freeing up memory of an object.Here is my code:
void Gateway::connect(DWORD dwIP)
{
if (m_objRRSInterface != NULL)
{
//delete m_obj;
m_obj = NULL;
}
m_obj = new objClass();
m_obj->SetCallBackFn(fncp);
if (m_obj->OpenSocket(dwIP, 3002))//3002 -port number
{
m_bConnect = TRUE;
}
else
{
m_bConnect = FALSE;
delete m_objRRSInterface;
m_obj = NULL;
}
}
objClass is not my own class , it is imported from an external .dll.
OpenSocket method opens a socket connection on port 3002 and then I get all the data on fncp.
This function work’s OK for the first time that i call it.
The problem appears when I call the function the second time.The problem that I have is that there is no CloseSocket method that i could call to reliable close the socket.
My question to you guys is that :Is there any method to dispose of an object and all this object dependences?
I’ve tried calling delete m_obj; but this hangs the application.
You should investigate about C++ destructors, which are meant to do what you are after.
This is where resources clean-up is usually done, but this is up to the programmer of the class. In other words, it is likely that
objClassdestructor does it resources clean-up there, but without reading the docs or the code, I cannot say.The fact that your application hangs has nothing to do with C++ or destructors in themselves, anyway. Rather, it seems a question of the way you use your DLL, like calling
deleteat the wrong time, or before some manual clean-up.But without knowing about
objClassinterface and semantics, I cannot help with this.