xlsxio icon indicating copy to clipboard operation
xlsxio copied to clipboard

can not use both read/write dll in one exe?

Open fengcai opened this issue 7 years ago • 10 comments

I wirte one test.exe agains libxlsxio dll, it's working well respectively on libxlsxio_read.dll and libxlsxio_write.dll but if I combine write and read in one application, error will report

environment windows 7 vc2012 lib: libxlsxio_read.dll.a/libxlsxio_write.dll.a

sample code:

#include <xlsxio_read.h>
#include <xlsxio_write.h>
#include <stdio.h>
#include <string>
#include <Windows.h>
using namespace std;

int read_xlsx() {
  //open .xlsx file for reading
xlsxioreader xlsxioread;
if ((xlsxioread = xlsxioread_open("d:\\0801_test.xlsx")) == NULL) {
  fprintf(stderr, "Error opening .xlsx file\n");
  return 1;
}

//read values from first sheet
char* value;
xlsxioreadersheet sheet;
const char* sheetname = NULL;
printf("Contents of first sheet:\n");
if ((sheet = xlsxioread_sheet_open(xlsxioread, sheetname, XLSXIOREAD_SKIP_EMPTY_ROWS)) != NULL) {
  //read all rows
  while (xlsxioread_sheet_next_row(sheet)) {
    //read all columns
    while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
      printf("%s\t", value);
      //free(value);
    }
    printf("\n");
  }
  xlsxioread_sheet_close(sheet);
}

//clean up
xlsxioread_close(xlsxioread);

return 0;
}

int write_xlsx() {
//open .xlsx file for writing (will overwrite if it already exists)
xlsxiowriter handle;
if ((handle = xlsxiowrite_open("d:\\test_xlsx.xlsx", "MySheet")) == NULL) {
  fprintf(stderr, "Error creating .xlsx file\n");
  return 1;
}

//write column names
xlsxiowrite_add_column(handle, "Col1", 16);
xlsxiowrite_add_column(handle, "Col2", 0);
xlsxiowrite_next_row(handle);

//write data
int i;
for (i = 0; i < 1000; i++) {
  xlsxiowrite_add_cell_string(handle, "test");
  xlsxiowrite_add_cell_int(handle, i);
  xlsxiowrite_next_row(handle);
}

//close .xlsx file
xlsxiowrite_close(handle);

return 0;
}

int main() {
  read_xlsx();
  write_xlsx();

return 0;
}

fengcai avatar Dec 14 '18 12:12 fengcai

What's the error? Any chance you can debug?

brechtsanders avatar Dec 14 '18 13:12 brechtsanders

Just tried your code with MinGW on Windows and it works perfectly. No crashes or errors.

brechtsanders avatar Dec 14 '18 16:12 brechtsanders

VC project zip file xlsxio.zip

project settings: xlsx_config error prompt xlsx_error_write

error information: The procedure entry point xlsxiowrite_add_cell_int could not be located in the dynamic library libxlsxio_read.dll

if the source code only contains read or wirte function, it's working well.

another issue for the test is I have to comment out the free(vlaue) as follows or it will crash

    while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
      printf("%s\t", value);
      //free(value);
    }

fengcai avatar Dec 15 '18 01:12 fengcai

hi, I find it is same as #38 and I solved it by lib created manually.

but the free-cause crash is still there in read function

    while ((value = xlsxioread_sheet_next_cell(sheet)) != NULL) {
      printf("%s\t", value);
      //free(value); // heap issue
    }

callstack is

>	msvcr110d.dll!_CrtIsValidHeapPointer(const void * pUserData) Line 2036	C++
 	msvcr110d.dll!_free_dbg_nolock(void * pUserData, int nBlockUse) Line 1322	C++
 	msvcr110d.dll!_free_dbg(void * pUserData, int nBlockUse) Line 1265	C++
 	msvcr110d.dll!free(void * pUserData) Line 49	C++
 	xlsx.exe!read_xlsx() Line 59	C++

fengcai avatar Dec 15 '18 04:12 fengcai

I just changed CMakeLists.txt so it also generates .def files when building with MinGW. In the next release the .def files will be included, allowing you to create proper .lib files for MSVC. What version of XLSX I/O did you use when you got the free-cause crash?

brechtsanders avatar Dec 15 '18 16:12 brechtsanders

latest version: xlsxio-0.2.19-binary-win32.zip from here

fengcai avatar Dec 16 '18 00:12 fengcai

The code you posted earlier doesn't have read_xlsx() on line 59. Can you attach the actual file that you were debugging?

brechtsanders avatar Dec 16 '18 10:12 brechtsanders

Also, can you try with version 0.2.21, which was just released? The binary packages for Windows now also include .def files and README.md explains how to make .lib files from them for use with MSVC.

brechtsanders avatar Dec 16 '18 11:12 brechtsanders

vc 2012 project: xlsxio-0.2.21-win32.zip I have tested the latest 0.2.21, crash as picture attached VC2012 Win7 crash

fengcai avatar Dec 17 '18 01:12 fengcai

The free() seems to be the same as in #73 Can you you try again with version 0.2.29 using xlsxioread_free() instead of free()? Thanks for letting me know if the issue is resolved now.

brechtsanders avatar Jul 10 '20 14:07 brechtsanders