简体   繁体   中英

Swig deferencing python temporaries in typemap

I want to pass a Python datetime object to the function printme below, seamlessly.

struct DateTime {
    uint64_t epochns;
};
void printme(DateTime dt);

So far I was able to create this SWIG typemap to convert from datetime to DateTime

%typemap(in)  DateTime
{
    PyObject* str = PyString_FromString( "timestamp" );
    PyObject* obj = PyObject_CallMethodObjArgs( $input, str, nullptr );
    $1.epochns = PyLong_AsLong( obj );
}

My question is: am I leaking the variables str and obj ? Do I need to dereference them?

Both PyString_FromString and PyObject_CallMethodObjArgs are documented to return a new reference, so both str and obj must be released via Py_DECREF or Py_XDECREF or you will leak two references.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM