I'm trying to call a C library in some C++ code. For simplicity let's say that the C library behaves as
utility_class data;
callback(double input[], void* data);
void* mem;
c_library_init(mem);
c_library_data(mem, &data);
c_library_callback(mem, &callback);
c_library_action(mem);
c_library_free(mem);
I then define a callback in C++ as
extern "C"
void callback(double input[], void* data) {
}
If callback is empty then the code runs fine, but if I try to allocate a standard vector,
extern "C"
void callback(double input[], void* data) {
std::vector<double> temp(5);
}
then I get a malloc error (c_library_callback apparently calls callback at least once with no problem but errs after two or three calls),
main(33158,0x7fff75ea5310) malloc: *** error for object 0x7fe71ac0cf08:
incorrect checksum for freed object - object was probably modified after being
freed.
Inspecting the memory with Allocations shows that objects are allocated at 0x7fe71ac0cf00 and 0x7fe71ac0cf10, but not at 0x7fe71ac0cf08 so it looks like memory corruption of some sort. I'm not even sure why free is being called in the C++ function. Moreover, this only seems to happen for some callbacks but not others.
Any suggestions on how to debug what's going on at the interface of the C and C++? Diagnostics instrumented directly in the code or through Xcode's Instruments are the most convenient, but I'll try anything. Thanks!