[C++-sig] free() of null-pointing handle<> throws SIGABRT

Martin Hellmich mhellmic at cern.ch
Fri Nov 23 20:36:18 CET 2012


Hi,

in my exception handling (based on a tutorial, which I can dig up if it 
helps), I create handles for the exc, val, tb values of an exception 
thrown in python.
The code is shown below in extractException(). It is a shortened 
version, which does not cover all possible cases.

In some cases, val is defined, but tb is not. When then htb points to 
0x0, the function fails with a SIGABRT as it returns.
I believe that the free() for the htb fails, when it goes out of scope 
at the end of the function.

I put the Error message and the GDB output at the end of the mail.
The address at which the free() fails is likely the one of tb, as it 
sits inbetween exc and val in memory.

Other calls to extractException() where tb is not null do not abort.

Do you have an idea why the free() could fail?
I am happy to post more information, if needed and would welcome any 
hints :)

Cheers
Martin

void PythonExceptionHandler::extractException() throw (DmException)
{
   using namespace boost::python;

   PyObject *exc,*val,*tb;
   PyErr_Fetch(&exc,&val,&tb);
   PyErr_NormalizeException(&exc,&val,&tb);
   handle<> hexc(exc),hval(allow_null(val)),htb(allow_null(tb));

   int code = 0;
   std::string what = "";

   if (PyObject_HasAttrString(val, "code") && 
PyObject_HasAttrString(val, "what")) {
     object oval(hval);
     extract<int> get_code(oval.attr("code")());
     extract<std::string> get_what(oval.attr("what")());
     code = get_code();
     what = get_what();
     throw DmException(code, what);
   } else {
     std::string excString;
     excString = extract<std::string>(str(hexc));
     throw DmException(DMLITE_SYSERR(DMLITE_UNKNOWN_ERROR),
                     excString);
   }
}

Error Message:
*** glibc detected *** /usr/lib64/dmlite/test/cpp/test-replicas: free(): 
invalid pointer: 0x0000003a4c5a0d60 ***

GDB output:
(gdb) p htb
$6 = {m_p = 0x0}
(gdb) p tb
$7 = (PyObject *) 0x0
(gdb) info locals
val = 0x3a4c5a0d80
tb = 0x0
hexc = {m_p = 0x3a4c579ea0}
what = ""
exc = 0x3a4c579ea0
hval = {m_p = 0x3a4c5a0d80}
htb = {m_p = 0x0}
code = <value optimized out>

-- 
Martin Hellmich                    Information Technology Department
mhellmic at cern.ch                                                CERN
+41 22 76 765 26                                   CH-1211 Geneva 23


More information about the Cplusplus-sig mailing list