-1

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

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
Hajducak Marek
  • 11
  • 1
  • 10
  • 3
    you don't free the memory, it is not given to you, you use the pointer to copy the string. – dgsomerton May 07 '18 at 06:47
  • 3
    "_How i free memory after const char* malloc?_" Where is the `malloc` in the shown code? If you don't know, that it was allocated with `malloc`, why are you trying to delete it with `free`? – Algirdas Preidžius May 07 '18 at 06:53
  • doesn't look like there is any way to release the memory, find a better excel library – Alan Birtles May 07 '18 at 06:57
  • This is the definition of method readStr: 'const wchar_t* readStr(int row, int col, Format** format = 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). Returns NULL if specified cell doesn't contain string or error occurs. Get error info with Book::errorMessage(). This must allocated memory, When I read by one by row in exel, After 158 time stop me reading any row :( – Hajducak Marek May 07 '18 at 07:22
  • 1
    Maybe the memory is been used up in other code. – Robert Andrzejuk May 07 '18 at 07:38
  • @RobertAndrzejuk That would be my assumption, too. 158 times reading a cell content, how many bytes could that be? Even if each string occupied 2000 bytes, we'd use less then 400 *kilo* bytes, i. e. not even half a *mega* byte. In times of several *giga* bytes of memory available... – Aconcagua May 07 '18 at 07:44
  • I have sheet with 32345 rows and 25 colloms. Its big date system I must read every row in this sheet in the program!!! – Hajducak Marek May 07 '18 at 07:55
  • The problem might not be in the reading of the data, only: what do You do with the data afterwards? – Robert Andrzejuk May 07 '18 at 07:56
  • I have data about every village in Slovak Republic, Arable areas, water areas and so on.. And I must save this data on the program. I save must dave the data on the table structure where first structure i Table with size an pointer to arrayList in the arrayList are pointer to TableNode witch contains a key such as "Bratislava" and pointer to data (my case is Village object where all data about village is stored). When I want find any village I use table->findDataOfKey("Bratislava"). – Hajducak Marek May 07 '18 at 08:06
  • ...Thats the problem when I read name with sheet->readStr(row, col) this is the type of const char* and when I save data with this type, When I try finde Data wit "Bratislava" key Not finde anything. I must convert const char* to std::string, AND there is the thing, when I use first time sheet->readStr(row, col) in the read variable is std::string such as "Bratislava" when I use second time its there const char*. I don't know how this work. – Hajducak Marek May 07 '18 at 08:06
  • @HajducakMarek "_This must allocated memory_" Sure.. My question was, why do you think that it is allocated with `malloc`? It can be, just as easily, allocated with `new`, and `free`ing what was `new`ed is undefined behavior. – Algirdas Preidžius May 07 '18 at 08:16
  • 25*32*1024, i. e. 800 k cells, what is the average data/string length? Let's for simplicity consider 128 bytes metadata and average string length of 256, we get around 300 Mb of data. Making copies of each cell, 600 Mb (and that's not stopping after 158 rows... and normally, meta data would not be copied either!). Don't see why your system should not be able to cope with this, unless there is some memory limitation for processes. As far as I see, you have to set these *explicitly*, though: – Aconcagua May 07 '18 at 08:20
  • https://blogs.technet.microsoft.com/clinth/2012/10/11/can-a-process-be-limited-on-how-much-physical-memory-it-uses/ or https://stackoverflow.com/questions/192876/set-windows-process-or-user-memory-limit – Aconcagua May 07 '18 at 08:20
  • Well, the call to `free` definitely is wrong, that's what all we commenters agree on. However, you skipped some code - have you considered that you might be producing the issue in what you replaced with the first ellipsis? – Aconcagua May 07 '18 at 08:33

2 Answers2

0

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.

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
-1

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

  • 1
    Welcome to stackoverflow. Please try to post more articulate answers, here for instance by specifying why here the problem are not memory leaks. Your answer is more a comment than a proper answer (I know you cannot comment yet, but still). – Giogre Jan 22 '21 at 06:22