How i free memory after const char* malloc?

402 Views Asked by At

I have problem with memory leaks

I have loop which read data on EXCEL with LibXL library.

    Book* book3 = xlCreateXMLBook();

    if (book3->load("Výmera Územia, využitie pôdy.xlsx")) {
        CellType cellType;
        Sheet* sheet = book3->getSheet(0);
        while (startIndex <= 100 * countOfLoad) {
            int k = 1;
            int numberOfBlank = 0;
            const char* name = sheet->readStr(startIndex, 0);
            nameOfVillage = name;
            free ((void*) name);
            ...
       }
       ...
   }

const char* name = sheet->readStr(startIndex, 0); - Reads a string and its format from cell.

Memory is allocated internally and valid until a new workbook is loaded or Book::release() is called for binary implementation (xls).

But it's needed to copy a result string every time in xml implementation (xlsx).

BUT When I write free ((void*) name) Give me Error:

Test(24919,0x1025bb380) malloc: *** error for object 0x10dacb738: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

When my loop going after 158 time reading a string, This program stop reading cause memory is full, I must some delete memory after reading string.

Anyone can help? THX

2

There are 2 best solutions below

0
Robert Andrzejuk On

From the documentation of readStr (http://libxl.com/spreadsheet.html#readStr)

Memory is allocated internally and valid until a new workbook is loaded or Book::release() is called for binary implementation (xls). But it's needed to copy a result string every time in xml implementation (xlsx).

Which means you need to call Book::release() when you are finished with the workbook to free up this memory.

So don't call free on pointers you receive from this library.

-Or- file a bug with the authors of LibXL.

-Or- use more memory from the freestore for your variables.

1
 OJ Tube On

I had the same problem. this is Probably not a problem with memory leaks.. the problem will be solved by purchasing a license.