对象问题的处理

发布于 2024-11-09 21:41:22 字数 775 浏览 0 评论 0原文

我在释放对象内存时遇到问题。这是我的代码:

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 不是我自己的类,它是从外部 .dll 导入的。
OpenSocket 方法在端口 3002 上打开一个套接字连接,然后我获取 fncp 上的所有数据。

我第一次调用这个函数时工作正常。
当我第二次调用该函数时,问题出现了。我遇到的问题是没有可以调用的 CloseSocket 方法来可靠地关闭套接字。

我向你们提出的问题是:是否有任何方法可以处理对象以及所有这些对象依赖项?
我尝试调用 delete m_obj; 但这会挂起应用程序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-11-16 21:41:22

您应该研究一下 C++ 析构函数,它们旨在完成您所追求的任务。
这是通常完成资源清理的地方,但这取决于类的程序员。换句话说, objClass 析构函数很可能会在那里进行资源清理,但如果不阅读文档或代码,我就不能说。

无论如何,应用程序挂起的事实与 C++ 或析构函数本身无关。相反,这似乎是您使用 DLL 的方式的问题,例如在错误的时间或在某些手动清理之前调用delete
但如果不了解 objClass 接口和语义,我无法对此提供帮助。

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 objClass destructor 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 delete at the wrong time, or before some manual clean-up.
But without knowing about objClass interface and semantics, I cannot help with this.

丿*梦醉红颜 2024-11-16 21:41:22

如果库文档中没有显式清理对象或关闭套接字的函数,那么在一定时间后没有活动时是否会自动关闭套接字?

如果您有办法判断套接字是否仍处于打开状态,则可以将对象传递给辅助线程,以便在检测到套接字已关闭时将其删除。

我唯一能想到的另一件事是,可以为新连接重用该对象。

If there is no function to explicitly clean up the object or close the socket in the library documentation, does it automatically shut down the socket if there is no activity after a certain amount of time?

If you have a way of telling if the socket is still open, you could pass the object to a helper thread to delete it when it detects that the socket is closed.

The only other thing that I can think of is that it may be possible to reuse the object for the new connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文