Changeset 49


Ignore:
Timestamp:
Aug 23, 2005, 12:45:05 PM (19 years ago)
Author:
dkearney
Message:

changed all functions with PyString_AsString calls to copy the contents
of the returned python object to newly allocated memory and return the
newly allocated memory. this leaves it up to the user to free all of
the returned items, while the interface function is still responsible
for taking care of python's memory.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cee/rappture_interface.h

    r47 r49  
    1313void**      rpChildren  (PyObject* lib, const char* path, const char* flavor);
    1414PyObject*   rpChildren_f  (PyObject* lib, const char* path, const char* flavor);
    15 const char* rpGet       (PyObject* lib, const char* path);
     15char* rpGet       (PyObject* lib, const char* path);
    1616void        rpPut       (PyObject* lib,
    1717                            const char* path,
  • trunk/src/cee/rappture_interface.c

    r47 r49  
    187187 *          1) the return object will need to be cast as a char*
    188188 *             to use it.
     189 *          2) It is the caller's responsibility to free the returned pointer
    189190 *       
    190  *        The Arguments lib and pathare not optional.
     191 *        The Arguments lib and path are not optional.
    191192 *          If either value is NULL, NULL will be returned.
    192193 *
     
    200201    PyObject* mbr_fxn   = NULL;      /* pointer to fxn of class lib */
    201202    PyObject* rslt      = NULL;      /* results from fxn call */
     203    char* tmpRetVal     = NULL;      /* fxn return value */
     204    char* tmpString     = NULL;      /* fxn return value */
    202205    void* retVal        = NULL;      /* fxn return value */
     206    int retValSize      = 0;
    203207
    204208    if (lib && path) {
     
    236240                        // return a char*
    237241                        // convert the result to c style strings
    238                         retVal = (void*) PyString_AsString(rslt);
     242                        // retVal = (void*) PyString_AsString(rslt);
     243                        tmpRetVal = PyString_AsString(rslt);
     244                        if (tmpRetVal) {
     245                            // get str length, add 1 for null termination
     246                            retValSize = strlen(tmpRetVal) + 1;
     247                            tmpString = (char*) calloc(retValSize,sizeof(char));
     248                            if (tmpString) {
     249                                strncpy(tmpString,tmpRetVal,retValSize);
     250                                retVal = (void*) tmpString;
     251                            }
     252                            else {
     253                                // raise error, out of memory
     254                            }
     255                        }
    239256                        Py_DECREF(rslt);
    240257                    }
     
    280297 *          1) the return object will need to be case as a char*
    281298 *             to use it.
     299 *          2) It is the caller's responsibility to free the contents of
     300 *              the returned pointer (ie. free(*retVal))
    282301 *       
    283302 *        The Arguments lib and pathare not optional.
     
    299318    char** rslt_arr_c   = NULL;
    300319    void** rslt_arr  = NULL;
     320    char*  tmpRetVal    = NULL;
     321    char*  tmpString    = 0;
     322    int retValSize      = 0;
     323   
    301324
    302325    if (lib && path) {
     
    373396                                // we cannot deallocate the results of
    374397                                // PyString_AsString()
    375                                 rslt_arr_c[index] = PyString_AsString(list_item);
    376                                 index++;
     398
     399                                tmpRetVal = PyString_AsString(list_item);
     400                                if (tmpRetVal) {
     401                                    // get str length, add 1 for null termination
     402                                    retValSize = strlen(tmpRetVal) + 1;
     403                                    tmpString = (char*) calloc(retValSize,sizeof(char));
     404                                    if (tmpString) {
     405                                        strncpy(tmpString,tmpRetVal,retValSize);
     406                                        rslt_arr_c[index] = tmpString;
     407                                        tmpString = NULL;
     408                                        index++;
     409                                    }
     410                                }
     411
     412                                // rslt_arr_c[index] = PyString_AsString(list_item);
     413                                // index++;
    377414                               
    378415                            }
     
    517554 *          NULL will be returned.
    518555 *
    519  * Returns pointer (const char*) to the c style string representing the
     556 * Returns pointer (char*) to the c style string representing the
    520557 *          xml text provided by the path of the Rappture object if
    521558 *          successful, or NULL if something goes wrong.
    522559 *
    523  *          The return value's contents should not be changed because
    524  *          it is a pointer being borrowed from python's buffer space
    525  *          (hence the _const_)
    526  */
    527 const char* rpGet(PyObject* lib, const char* path)
     560 *          It is the responsibility of the caller to free the
     561 *          returned pointer
     562 *
     563 */
     564char* rpGet(PyObject* lib, const char* path)
    528565{
    529566    PyObject* mbr_fxn   = NULL;      /* pointer to fxn of class lib */
    530567    PyObject* rslt      = NULL;      /* results from fxn call */
     568    char* tmpRetVal     = NULL;      /* return value */
    531569    char* retVal        = NULL;      /* return value */
     570    int retValSize      = 0;
    532571
    533572    if (lib) {
     
    543582                if (rslt) {
    544583                    // convert the result to c style strings
    545                     retVal = PyString_AsString(rslt);
    546                     // Py_DECREF(rslt);
     584                    tmpRetVal = PyString_AsString(rslt);
     585
     586                    if (tmpRetVal) {
     587                        // get str length, add 1 for null termination
     588                        retValSize = strlen(tmpRetVal) + 1;
     589                        retVal = (char*) calloc(retValSize,sizeof(char));
     590                        if (retVal) {
     591                            strncpy(retVal,tmpRetVal,retValSize);
     592                        }
     593                        else {
     594                            // raise error, out of memory
     595                        }                           
     596                    }
     597
     598                    Py_DECREF(rslt);
    547599                }
    548600               
     
    555607    }
    556608
    557     return (const char*) retVal;
     609    return retVal;
    558610   
    559611}
     
    737789    char* tmpretVal     = NULL;      /* return value */
    738790    char* retVal        = NULL;      /* return value */
     791    int retValSize      = 0;
    739792
    740793    if (lib) {
     
    751804                    // convert the result to c style strings
    752805                    tmpretVal = PyString_AsString(rslt);
    753                     retVal = (char*) calloc(strlen(tmpretVal),sizeof(char));
    754                     strcpy(retVal,tmpretVal);
     806                    if (tmpretVal) {
     807                        // get str length, add 1 for null termination
     808                        retValSize = strlen(tmpretVal) + 1;
     809                        retVal = (char*) calloc(retValSize,sizeof(char));
     810                        if (retVal) {
     811                            strncpy(retVal,tmpretVal,retValSize);
     812                        }
     813                        else {
     814                            // raise error, out of memory
     815                        }
     816                    }
    755817                    Py_DECREF(rslt);
    756818                }
  • trunk/src/fortran/rappture_fortran.c

    r47 r49  
    514514                        }
    515515                        *(retText+length_out-1) = ' ';
    516 
     516                       
     517                        free(retObj);
     518                        retObj = NULL;
    517519                    }
    518520                    else {
Note: See TracChangeset for help on using the changeset viewer.