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<char> 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.