【问题标题】:rapidJson: crashed in release moderapidJson:在发布模式下崩溃
【发布时间】:2017-07-31 21:01:03
【问题描述】:

我使用rapidJson 读取json数据。我可以在调试和发布模式下构建我的应用程序,但应用程序在发布模式下崩溃。

    using namespace rapidjson;
    ...
    char *buffer;
    long fileSize;
    size_t fileReadingResult;

    //obtain file size
    fseek(pFile, 0, SEEK_END);
    fileSize = ftell(pFile);
    if (fileSize <= 0) return false;
    rewind(pFile);

    //allocate memory to contain the whole file
    buffer = (char *)malloc(sizeof(char)*fileSize);
    if (buffer == NULL) return false;

    //copy the file into the buffer
    fileReadingResult = fread(buffer, 1, fileSize, pFile);
    if (fileReadingResult != fileSize) return false;
    buffer[fileSize] = 0;

    Document document;
    document.Parse(buffer);

当我在发布模式下运行它时,我遇到了一个Unhanded exception; A heap has been corrupted。 应用程序在 malloc.c 文件中的 "res = _heap_alloc(size) 处中断

void * __cdecl _malloc_base (size_t size)
{
     void *res = NULL;

//  validate size
if (size <= _HEAP_MAXREQ) {
    for (;;) {

        //  allocate memory block
        res = _heap_alloc(size);

        //  if successful allocation, return pointer to memory
        //  if new handling turned off altogether, return NULL

        if (res != NULL)
        {
            break;
        }
        if (_newmode == 0)
        {
            errno = ENOMEM;
            break;
        }

        //  call installed new handler
        if (!_callnewh(size))
            break;

        //  new handler was successful -- try to allocate again
    }  

它在调试模式下运行良好。

【问题讨论】:

    标签: crash release rapidjson


    【解决方案1】:

    也许它可能是您的Mallocmemory leak 问题,因为它有一次在调试中运行良好,但是当您让应用程序运行更长时间时它会崩溃。

    你用过free你的buffer吗?

    【讨论】:

      【解决方案2】:

      原因很简单。您分配了一个 fileSize 字节的缓冲区,但在读取文件后,您使用 buffer[fileSize] = 0; 在 fileSize+1-th 位置写入

      修复:将分配更改为更大。

      buffer = (char *)malloc(fileSize + 1);
      

      Debug 使用额外的字节构建填充内存分配,因此它不会崩溃。

      【讨论】:

        猜你喜欢
        • 2021-07-17
        • 1970-01-01
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 2016-02-05
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多