Python ctypes pointer and allocated memory

Pep

Let's say I want to pass a pointer to int to a C function from Python using ctypes:

from ctypes import *
lib = CDLL("./myLib.so")
i = c_int(50)
pi = pointer(i)
lib.myfunc(pi)

No problem with this code. But let's say I do this instead:

from ctypes import *
lib = CDLL("./myLib.so")
pi = pointer(c_int(50))
lib.myfunc(pi)

What happens regarding the garbage collector? In the first example the pointed contents were referenced through the i variable. But I'm not sure if they are in the second one. There is no variable referencing those contents. Can it be garbage collected? Or the sole fact that the pointer is pointing at it makes the contents safely allocated?

ead

When a pointer-object is created, it keep a reference of the original object, thus preventing it of being destructed.

You can verify it with the following code:

from ctypes import *
import sys
i = c_int(50)
print(sys.getrefcount(i)) # => 2 - a reference from i and one as argument of sys.getfrefcount
pi = pointer(i)
print(sys.getrefcount(i)) # => 3 - additional reference is saved in pi
del pi
print(sys.getrefcount(i)) # => 2 again - the reference from pi was cleared

that means, also in the second version there will be no dangling pointer and it save to use.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python print() corrupting memory allocated by ctypes

Returning pointer to allocated memory?

Allocated memory size by pointer in RPGLE

Is setting pointer to null an allocated memory?

Is it undefined behaviour to use pointer after allocated memory?

Null Pointer Error with pre-allocated memory?

where is placed the this pointer and and when memory is allocated for it?

Using memory allocated pointer inside the function

Is there a downside to declaring a pointer used for allocated memory as const

Undefined behavior with pointer arithmetic on dynamically allocated memory

Getting input into dynamically allocated memory then returning the pointer

How to free memory allocated on double pointer struct

Python Ctypes passing pointer for data

Python Bytes to CTypes Void Pointer

python ctypes - wrap void pointer

Pointer argument passing in python ctypes

when memory is allocated to variable in python?

How is memory allocated for variables in Python?

If a parameter is a pointer type, is the parameter a pointer allocated in local memory

Will freeing a structure pointer also free the memory allocated inside of the structure in C?

What are the resources managed by smart pointer while their memory are not allocated by new?

Why is there not a function in C that returns size of memory allocated pointed to by a pointer?

Is it possible to profile memory only by storing allocated pointer address in C++?

There is no enough memory allocated for double pointer but still i can allocate

Can I delete a memory previously allocated dynamically, but with a different pointer?

Pointer access beyond allocated memory does'nt cause segfault

How to free memory allocated in a function without returning its pointer?

Should we use delete[] or delete on function that return allocated memory pointer?

how to specify a JNR Pointer like that of python ctypes