Commit f1c6453f authored by Kelvin Rodriguez's avatar Kelvin Rodriguez Committed by Jesse Mapel
Browse files

Segfault fix (#358)

* release bump

* added py_incref to borrowed references
parent 2a7a2052
Loading
Loading
Loading
Loading
+16 −7
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@ namespace ale {
                           "Check if Installed correctly and the function ale.loads exists.");
     }
      

     // Create a Python tuple to hold the arguments to the method.
     PyObject *pArgs = PyTuple_New(3);
     if(!pArgs) {
@@ -94,16 +95,19 @@ namespace ale {
     // Set the Python int as the first and second arguments to the method.
     PyObject *pStringFileName = PyUnicode_FromString(filename.c_str());
     PyTuple_SetItem(pArgs, 0, pStringFileName);
     Py_INCREF(pStringFileName); // take ownership of reference

     PyObject *pStringProps = PyUnicode_FromString(props.c_str());
     PyTuple_SetItem(pArgs, 1, pStringProps);
     Py_INCREF(pStringProps); // take ownership of reference
     
     PyObject *pStringFormatter = PyUnicode_FromString(formatter.c_str());
     PyTuple_SetItem(pArgs, 2, pStringFormatter);
     Py_INCREF(pStringFormatter); // take ownership of reference 
    

     // Call the function with the arguments.
     PyObject* pResult = PyObject_CallObject(pFunc, pArgs);

     if(!pResult) {
        throw invalid_argument("No Valid instrument found for label.");
     }
@@ -114,15 +118,20 @@ namespace ale {
     if(!temp_bytes){
       throw invalid_argument(getPyTraceback());
     }
     
     std::string cResult;
     
     char *temp_str = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
     cResult = temp_str; // copy into std::string

     Py_DECREF(pResultStr);

     Py_DECREF(pStringFileName);
     Py_DECREF(pStringProps);
     Py_DECREF(pStringFormatter);
     
     Py_DECREF(pArgs);

     return cResult;
 }