为什么free()不断触发断点

时间:2018-06-23 15:10:11

标签: c breakpoints free

有人可以告诉我为什么我的free()函数不断触发断点吗?然后,我收到一个Microsoft Visual C ++运行时库错误:调试断言失败!程序:... .exe,文件:minkernel \ crts \ ucrt \ src \ appcrt \ heap \ debug_heap.cpp ...表达式:_CrtlsValidHeapPointer(block)。

#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "IPHLPAPI.lib")


int main()
{

IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(IP_ADAPTER_INFO));

if (pAdapterInfo == NULL)
{
    return 1;
}

ULONG size = sizeof(*pAdapterInfo);
ULONG * pOutBufLen = &size;

if(GetAdaptersInfo(pAdapterInfo, pOutBufLen) == ERROR_BUFFER_OVERFLOW) //When this error code is returned, the pOutBufLen parameter points to the required buffer size.
{
    pAdapterInfo = realloc(pAdapterInfo, *pOutBufLen);
}
else
{
    return 1;
}

if(GetAdaptersInfo(pAdapterInfo, pOutBufLen) == NO_ERROR)
{
    while (pAdapterInfo->Next != NULL)
    {
        printf("%s\n", pAdapterInfo->Description);
        pAdapterInfo = pAdapterInfo->Next;
    }

}

free(pAdapterInfo);


getchar();

return 0;

}

解决方案:好的,谢谢大家的回答。我用另一个称为pAdapter的指针解决了我的问题:

int main()
{
IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(IP_ADAPTER_INFO));
IP_ADAPTER_INFO *pAdapter = NULL; //for memory freeing purposes only
...

if(GetAdaptersInfo(pAdapterInfo, pOutBufLen) == NO_ERROR)
{
    pAdapter = pAdapterInfo;
    while (pAdapter->Next != NULL)
    {
        printf("%s\n", pAdapter->Description);
        pAdapter = pAdapter->Next;
    }

}

free(pAdapterInfo);

0 个答案:

没有答案