My function purpose is to make custom sized array and pass it to python. Problem is everytime I try to do so, I get
Error in `python3': double free or corruption (fasttop)
or something similar.
I have produced a minimal example of such bug (I built tst.so shared library):
for c++:
int * lala = new int[1];
void def()
{
delete[] lala;
int * lala = new int[100];
}
extern "C" int * abc()
{
def();
return lala;
}
for python:
import ctypes
import numpy as np
from numpy.ctypeslib import ndpointer
import inspect
from os.path import abspath, dirname, join
fname = abspath(inspect.getfile(inspect.currentframe()))
libIII = ctypes.cdll.LoadLibrary(join(dirname(fname), 'tst.so'))
abc = libIII.abc
abc.restype = ndpointer(dtype=ctypes.c_int, shape=(100,))
abc.argtypes= None
asd = np.reshape(np.frombuffer(abc(), dtype = np.uint16), (100))
asd = np.reshape(np.frombuffer(abc(), dtype = np.uint16), (100))
asd = np.reshape(np.frombuffer(abc(), dtype = np.uint16), (100))
if I make int * lala = new int[100]; (same size as it should be), everything works.
Am I doing something wrong? How should I delete old array and make other with different size?