wishel 发表于 2010-8-3 17:14:34

vs2005开发,Out of memory对话框的问题

有一段代码,运行时常跳出Out of memory对话框。
添加try{}catch(...) {}后,不再重现。

查了半天,也没查到这个Out of memory对话框指的是什么含义?如果是内存泄露的话,不应该被try抓到啊?难道是vs定义的一种特殊异常?

Jack 发表于 2010-8-3 19:18:07

字面上好像是你分配内存太多,导致内存不够,抛出异常的问题...
可以使用下面这段代码,看看倒底这个异常是怎么描述的.
        catch (std::bad_alloc& e)
        {
                std::cerr << "Exception: " << e.what() << "\n";
        }

wishel 发表于 2010-8-4 10:51:57

本帖最后由 wishel 于 2010-8-4 10:53 编辑

真杯具,改成这样
catch (std::bad_alloc& e) {
                FILE *fp = fopen( "exception.txt", "a+" );
                if( fp != NULL )
                {
                        fprintf( fp, "Exception1: %s\n",e.what());
                        fclose(fp);
                }
      }
      catch (std::exception& e) {
                FILE *fp = fopen( "exception.txt", "a+" );
                if( fp != NULL )
                {
                        fprintf( fp, "Exception2: %s\n",e.what());
                        fclose(fp);
                }
      }
      
      catch(...)
                {
                        FILE *fp = fopen( "exception.txt", "a+" );
                        if( fp != NULL )
                        {
                              fprintf( fp, "Exception3: %s\n", __FUNCTION__);
                              fclose(fp);
                        }
                }

最后打出来的是Exception3

到底Out of memory是什么异常啊,ms连个文档也没有。真是受不了。。

Jack 发表于 2010-8-4 13:56:33

杯具,楼主能贴代码么?

wishel 发表于 2010-8-4 15:16:27

找到初步原因了,是因为
int nRealLen = ReceiveFrom(buffer, sizeof buffer, (SOCKADDR*)&sockAddr, &iSockAddrLen);
ProcessPacket(buffer+1, nRealLen-1, sockAddr.sin_addr.S_un.S_addr, sockAddr.sin_port);

ProcessPacket()中根据第二个参数 nRealLen-1, new 了一个这个size的内存。
但是ReceiveFrom没有做判断是否出错,出错的话返回值是-1,转为无符号整数那就是超大。

wishel 发表于 2010-8-4 15:22:21

但是又有个奇怪的问题,
我加了一句条件判断:
if(nRealLen == SOCKET_ERROR)
    printf("error is %d\n", CAsyncSocket::GetLastError());

结果打印出的error是131和183两种,但查winerror.h

//
// MessageId: ERROR_NEGATIVE_SEEK
//
// MessageText:
//
// An attempt was made to move the file pointer before the beginning of the file.
//
#define ERROR_NEGATIVE_SEEK            131L

//
// MessageId: ERROR_ALREADY_EXISTS
//
// MessageText:
//
// Cannot create a file when that file already exists.
//
#define ERROR_ALREADY_EXISTS             183L


而msdn上关于ReceiveFrom error的文档只有以下几种
If no error occurs, ReceiveFrom returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

WSANOTINITIALISED   A successful AfxSocketInit must occur before using this API.

WSAENETDOWN   The Windows Sockets implementation detected that the network subsystem failed.

WSAEFAULT   The lpSockAddrLen argument was invalid: the lpSockAddr buffer was too small to accommodate the peer address.

WSAEINPROGRESS   A blocking Windows Sockets operation is in progress.

WSAEINVAL   The socket has not been bound with Bind.

WSAENOTCONN   The socket is not connected (SOCK_STREAM only).

WSAENOTSOCK   The descriptor is not a socket.

WSAEOPNOTSUPP   MSG_OOB was specified, but the socket is not of type SOCK_STREAM.

WSAESHUTDOWN   The socket has been shut down; it is not possible to call ReceiveFrom on a socket after ShutDown has been invoked with nHow set to 0 or 2.

WSAEWOULDBLOCK   The socket is marked as nonblocking and the ReceiveFrom operation would block.

WSAEMSGSIZE   The datagram was too large to fit into the specified buffer and was truncated.

WSAECONNABORTED   The virtual circuit was aborted due to timeout or other failure.

WSAECONNRESET   The virtual circuit was reset by the remote side.

wishel 发表于 2010-8-4 15:23:36

现在麻烦是,找不到出错的具体原因啊

winston 发表于 2010-8-4 16:52:57

用WSAGetLastError判断。

freeeyes 发表于 2010-8-4 16:58:54

ReceiveFrom貌似有两种,一种是错误会返回-1,还有一种好像是有0的时候。记不清了,好像是正常断开的时候。建议版主把nRealLen-1每次都打印出来。如果是Out of memory错误一般是new了一个超大内存引起的。

wishel 发表于 2010-8-4 18:00:28

用WSAGetLastError判断。

泪流满面。。。还是131和183
第一次是131,后面全是183
页: [1] 2
查看完整版本: vs2005开发,Out of memory对话框的问题