On 10/3/2019 11:53 AM, Wei-min Pan wrote: > > On 10/3/2019 11:31 AM, Simon Marchi wrote: >> On 2019-10-03 2:21 p.m., Wei-min Pan wrote: >>> Let's use an example (checking omitted): >>> >>> We're replacing: >>>    name = ctf_type_aname_raw (fp, tid); >>>    TYPE_NAME (type) = obstack_strdup (&of->objfile_obstack, name); >>>    free (name); >>> >>> with >>>    gdb::unique_xmalloc_ptr name (ctf_type_aname_raw (fp, tid)); >>>    TYPE_NAME (type) = obstack_strdup (&of->objfile_obstack, name.get ()); >>> >>> >>> The allocated copy from ctf_type_aname_raw is not freed. Or did I >>> miss something? >>> >>> Weimin >> Yes, the second snippet frees the copy returned by ctf_type_aname_raw. >> >> It might just be that you are not familiar with the concept of >> std::unique_ptr in C++: >> >>    https://en.cppreference.com/w/cpp/memory/unique_ptr >> >> It's a wrapper around a simple pointer that automatically calls a >> deleter function >> when it goes out of scope.  gdb::unique_xmalloc_ptr is a >> specialization of std::unique_ptr >> that uses xfree as the deleter function. >> >> Here's a similar use: >> >> https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gdb/fbsd-tdep.c;h=9422e3c1a7e1a65c76b088f42bb5986bd13a089f;hb=HEAD#l1530 >> >> `fbsd_core_vnode_path` return a pointer to an allocated C String, >> which `cwd` wraps.  When >> it goes out of scope, `cwd` automatically calls `xfree` with the >> pointer. > > Oh, thanks for the explanation, reference and example. Will switch to > using gdb::unique_xmalloc_ptr<> instead. Hi Simon, I've switched to using gdb::unique_xmalloc_ptr, please see the attached patch, and ran the CTF test suite successfully. Will submit a revised patch upstream shortly. Thanks again for your reviews and help. Weimin