gpt4 book ai didi

c++ - visual studio 已触发断点 Poco NotificationCenter

转载 作者:行者123 更新时间:2023-11-30 05:48:58 25 4
gpt4 key购买 nike

我遇到的问题是我收到消息“VS 已触发断点”,当我在那里断点时,VS 跳转到 POCO NotificationCenter 的源代码: enter image description here

我正在使用 Poco 1.5.4。

调用堆栈中的前一个条目位于以下代码段中:

void WebSocketController::HandleReceivedMessages() {
AutoPtr<Notification> notification(receivedMessagesQueue.waitDequeueNotification());

while (!messageHandlerActivity.isStopped() && notification) {
MessageNotification* messageNotification = dynamic_cast<MessageNotification*>(notification.get());
if (messageNotification)
{
notificationCenter.postNotification(messageNotification);
}

notification = receivedMessagesQueue.waitDequeueNotification();
}
}

我在调用堆栈中看到的具体行(带有行号)如下:

notification = receivedMessagesQueue.waitDequeueNotification();

这是 MessageNotification.h 的代码:

    class MessageNotification : public Notification
{
public:
MessageNotification(Message *data);
~MessageNotification();
Message* GetData();
private:
Message *data;
};

这是 MessageNotification.cpp 的代码:

    MessageNotification::MessageNotification(Message *data) {
this->data = data;
}

MessageNotification::~MessageNotification() {
delete data;
data = nullptr;
}

Message* MessageNotification::GetData() {
return data;
}

在这里你可以看到 Message 类的构造函数:

        Message::Message(const MessageCommandEnum cmd, const string& to, StringMap *params, const string& data)
: cmd(cmd), to(to), data(data) {
this->params = params == nullptr ? new StringMap() : params;
}

Message::Message(const Message& msg) : to(msg.to), cmd(msg.cmd), data(msg.data) {
params = new StringMap(*msg.params);
}

Message::Message(const Message* msg) : to(msg->to), cmd(msg->cmd), data(msg->data) {
params = new StringMap(*msg->params);
}

Message::~Message() {
if (params != nullptr) {
delete params;
params = nullptr;
}
}

这个类中的其余方法只是 getters/setters。

知道为什么会这样吗?

我的研究告诉我,如果堆被破坏,就会出现此消息。但是我找不到应该发生这种情况的任何代码行。这种行为有点奇怪,因为当我在消息上按继续时,应用程序运行没有任何问题。当应用程序未在后台使用调试器启动时(例如,启动 Debug 文件夹中的 exe),我没有任何问题。

我仍在学习 C++,因此非常感谢任何反馈/帮助。

谢谢

最佳答案

WebSocketController::HandleReceivedMessages() 中的 AutoPtr 通知将在您为它分配另一个指针后立即被 AutoPtr 删除。但是,此时,该指针已传递到另一个 AutoPtr 中的 NotificationCenter,无论您稍后尝试取消引用(或 AutoPtr 尝试删除)它,都会导致未定义的行为。

将通知指针放入 AutoPtr 后,只需将其作为 AutoPtr 传递(使用 notification.cast () 进行转换)。 此外,永远不会传入 MessageNotification 构造函数指针参数,因为您永远不会构造 MessageNotification 对象(您只是将 Notification* 动态转换为 MessageNotification*)。

查看NotificationQueue example了解如何以正确的方式做到这一点。

关于c++ - visual studio 已触发断点 Poco NotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27964916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com