开发者

Why does my program halt when calling front() on a std::queue?

I want to use the Irrnet network library in an Irrlicht game.

The source code uses Linux sockets and I'm trying to port it for Windows replacing it with code that uses Windows' Winsock2.

The library compiles successfully but when I try to run the Quake example it crashes. I located the line at which the program stops but i can't figure out how to solve the problem.

The program stops at the second call of the function getNextItem

class NetworkType {
    public :
        NetworkType();
        ~NetworkType();
        template<class T>
        void getNextI开发者_如何学JAVAtem(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);
}

and exactly at this line

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

Can anyone tell me why does the program stop the second time it gets to this line ?

here is the link for the complete source code


I assume by "stops" you mean "crashes" in some fashion? Likely causes for a crash on the line in question are:

  • The NetworkType instance that is invoking the getNextItem() method is garbage (the this pointer is garbage or null). This could happen due to bad pointer math elsewhere, a premature delete or destruction of the instance, et cetera. This would manifest as a fault when the program attempted to access the items member.
  • The items container is empty. In these cases the return value of front() is undefined (since it is a reference) and the constructor for istringstream may be crashing. front() itself may be raising a debug/runtime check error as well depending on your compiler and its configuration.


Actually you might have a runtime error on this one if the dequeue is empty: MSDN deque

So just check the deque isn't empty before you try to pop a value from it.

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

[edit] confounded std and (I guess) MSDN ( OP doesn't say) lib.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜