我们将直接开展业务,以下代码段返回
Unhandled exception at 0x7786AA3C (ntdll.dll) in Project4.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7787FE38).
段:
while(true)
{
if(WSALookupServiceNext(lookup_handle,flags,&query_set_length,query_set)==NO_ERROR)
{
char buffer[40] = {0};
DWORD buffer_size = sizeof(buffer);
device_socket_address = (SOCKADDR_BTH*)query_set->lpcsaBuffer->RemoteAddr.lpSockaddr;
result = device_socket_address->btAddr;
WSAAddressToString(query_set->lpcsaBuffer->RemoteAddr.lpSockaddr,sizeof(SOCKADDR_BTH),NULL,buffer,&buffer_size);
fprintf(stdout,"found device: %s - %s\n", buffer,query_set->lpszServiceInstanceName);
break;
}
}
中断发生在WSAAddressToString
在WSALookupServiceNext之前我调用
//start the LookUp service for bluetooth devices
if(WSALookupServiceBegin(query_set,flags,&lookup_handle)==SOCKET_ERROR)
{
fprintf(stderr, "something went completely wrong... %d",WSAGetLastError());
system("PAUSE");
ExitProcess(2);
}
查询集初始化
DWORD query_set_length = sizeof(WSAQUERYSET);
DWORD flags = LUP_CONTAINERS | LUP_FLUSHCACHE | LUP_RETURN_NAME | LUP_RETURN_ADDR;
WSAQUERYSET query_set = (WSAQUERYSET*)malloc(query_set_length);
ZeroMemory(query_set,query_set_length);
query_set->dwSize = query_set_length;
query_set->dwNameSpace = NS_BTH;
query_set->dwNumberOfCsAddrs = 0;
更新:
问题的根源似乎是WSALookupServiceNext
,我怀疑我的句柄(lookup_handle)是以错误的方式传递的。
我的问题是,是什么导致了堆损坏,我该如何修复它?
我只是想找一个设备,并能够发送一个测试字符串。
答案 0 :(得分:0)
我已从我的标记
中删除了LUP_FLUSCACHE
和LUP_CONTAINERS
DWORD flags = LUP_RETURN_NAME | LUP_RETURN_ADDR;
并且仅在WSALookupServiceBegin
if(WSALookupServiceBegin(query_set,flags |= LUP_FLUSHCACHE | LUP_CONTAINERS,&lookup_handle)==SOCKET_ERROR)
{
fprintf(stderr, "something went completely wrong... %d",WSAGetLastError());
system("PAUSE");
ExitProcess(2);
}
似乎可以防止这个问题。
我刚看了这个MSDN entry
嘿嘿,我也用fprintf来stdin而不是stdout(我可以在永恒的火焰中燃烧)。