2

我想在 Irrlicht 游戏中使用 Irrnet 网络库。

源代码使用 Linux 套接字,我正在尝试将它移植到 Windows 上,用使用 Windows 的 Winsock2 的代码替换它。

该库编译成功,但是当我尝试运行 Quake 示例时它崩溃了。我找到了程序停止的那一行,但我不知道如何解决这个问题。

程序在函数getNextItem的第二次调用处停止

class NetworkType {
    public :
        NetworkType();
        ~NetworkType();
        template<class T>
        void getNextItem(irr::core::vector3d<T>& data);

    private:
        typedef std::queue<std::string> Container;
        Container items;
};

template<class T>
void NetworkType::getNextItem(irr::core::vector3d<T>& data) {
    T X, Y, Z;

    std::istringstream item(items.front());
    // the program does not get here the second time it calls this function
    items.pop();

    item >> X;
    item >> Y;
    item >> Z;

    data = irr::core::vector3d<T>(X, Y, Z);
}

正好在这条线上

  std::istringstream item(items.front());

谁能告诉我为什么程序第二次到达这条线时会停止?

这是完整源代码的链接

4

2 回答 2

4

我认为“停止”是指某种方式的“崩溃”?有问题的线路崩溃的可能原因是:

  • NetworkType调用该方法的实例是getNextItem()垃圾(this指针是垃圾或空)。这可能是由于其他地方的指针数学错误、实例的过早删除或破坏等原因而发生的。当程序试图访问该items成员时,这将表现为一个错误。
  • items容器是空的。在这些情况下, of 的返回值front()是未定义的(因为它是一个引用),并且 for 的构造函数istringstream可能会崩溃。front()根据您的编译器及其配置,它本身也可能引发调试/运行时检查错误。
于 2011-10-14T14:57:48.537 回答
1

实际上,如果出队为空,您可能会遇到运行时错误:MSDN deque

因此,在尝试从中弹出一个值之前,只需检查双端队列是否为空。

if(items.size()>0)
{
//do things
}
else
{
 //error deque empty
}

[编辑]混淆了标准和(我猜)MSDN(OP没有说)lib。

于 2011-10-16T14:55:27.210 回答