I'm using CFFI to generate a DLL:
import cffi
ffibuilder = cffi.FFI()
ffibuilder.embedding_api('''
char* get_string();
''')
ffibuilder.set_source('my_plugin', '')
ffibuilder.embedding_init_code('''
from my_plugin import ffi, lib
@ffi.def_extern()
def get_string():
val = "string"
return lib.strdup(val.encode())
''')
ffibuilder.cdef('''
char *strdup(const char *);
''')
ffibuilder.compile(target='my-plugin.*', verbose=True)
I generate the DLL by running this previous script. Now, I create this sample of C++ code to use my DLL:
#include <iostream>
#include <windows.h>
typedef char* (__stdcall *get_string_t)();
int main()
{
HINSTANCE hGetProcIDDLL = LoadLibrary("my-plugin.dll");
if (!hGetProcIDDLL) {
std::cout << "could not load the dynamic library" << std::endl;
return -1;
}
get_string_t get_string = (get_string_t)GetProcAddress(hGetProcIDDLL, "get_string");
if (!get_string) {
std::cout << "could not locate the function" << std::endl;
return -1;
}
char* val = get_string();
std::cout << "Value = " << val << std::endl;
free(val); // Crash !
std::cout << "End" << std::endl;
return 0;
}
I compile using the compiler of Visual Studio 2010 and, when I run my app, it crashes during the free
instruction:
> cl get_string.cpp
Compilateur d'optimisation Microsoft (R) 32 bits C/C++ version 16.00.40219.01 pour 80x86
Copyright (C) Microsoft Corporation. Tous droits réservés.
get_string.cpp
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning C4530: Gestionnaire d'exceptions C++ utilisé, mais les sémantiques de déroulement n'ont pas été activées. Spécifiez /EHsc
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:get_string.exe
get_string.obj
> get_string.exe
Value = string
I follow the indication given in this answer. What should I do to free the memory and avoid my app to crash? Indeed, if I remove the free
instruction, my app works well but it's not a clean solution.