why can i not free ctypes memory in c?

32 Views Asked by At

i have a python file that trys to free memory from an array created using ctypes:

import ctypes
import os

# Load the DLL
script_dir = os.path.dirname(os.path.abspath(__file__))
dll_path = os.path.join(script_dir, "test.dll")
funcs = ctypes.CDLL(dll_path)

funcs.free_arr.argtypes = [ctypes.POINTER(ctypes.c_int)]

ids = [1, 2, 3]
ids_arr = (ctypes.c_int * len(ids))(*ids)
funcs.free_arr(ids_arr)

My c file looks like this:

#include <stdio.h>
#include <stdlib.h>

void free_arr(int* ids) {
    free(ids);
}

However when i compile the c file and run the python file i get a code=3221226356 error. What is happening? Does python automatically free the array? I want to be able to free the array inside c, so that i have more control.

1

There are 1 best solutions below

0
Mark Tolonen On

Python owns the memory and frees it when the references to the Python object goes to zero. There’s more to the object than the memory it wraps.