diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index 743e6a6..25ae901 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -235,6 +235,7 @@ valpy_cast (PyObject *self, PyObject *args) struct type *type; struct value *res_val = NULL; /* Initialize to appease gcc warning. */ volatile struct gdb_exception except; + struct value *val=((value_object *) self)->value; if (! PyArg_ParseTuple (args, "O", &type_obj)) return NULL; @@ -248,11 +249,17 @@ valpy_cast (PyObject *self, PyObject *args) TRY_CATCH (except, RETURN_MASK_ALL) { - res_val = value_cast (type, ((value_object *) self)->value); + res_val = value_cast (type, val); } GDB_PY_HANDLE_EXCEPTION (except); - return value_to_value_object (res_val); + if(res_val == val) + { + Py_INCREF(self); + return self; + } + else + return value_to_value_object (res_val); } static Py_ssize_t @@ -793,7 +800,12 @@ valpy_float (PyObject *self) } /* Returns an object for a value which is released from the all_values chain, - so its lifetime is not bound to the execution of a command. */ + so its lifetime is not bound to the execution of a command. + DO NOT call with a value that is already stored in a value_object, as + this will eventually crash gdb. If necessary value_copy can be used + first. It may be better if values_in_python could track the PyObjects to + which they are assigned and this function could manipulate their + refcounts. */ PyObject * value_to_value_object (struct value *val) { -- 1.6.0.4