Allocating memory to pass back via ctypes callback function

Scott scott.pigman at gmail.com
Fri Jun 19 07:14:19 EDT 2009


I think I found the answer to my own question.  Can anyone spot any
issues with the following solution? The application I'm writing will
be hitting these callbacks pretty heavily so I'm nervous about mucking
up the memory management and creating one of those bugs that passes
undetected through testing but nails you in production.

def my_callback(p_cstring):
    answer = 'foobar'

    address = VENDOR_malloc(len(answer)+1)

    cstring = c_char_p.from_address( address )

    cstring.value = answer
    p_cstring.contents = cstring
    return



More information about the Python-list mailing list