diff --git a/src/symbols/dwarf/dwarf.hpp b/src/symbols/dwarf/dwarf.hpp index 835a7f9..6fb3bfc 100644 --- a/src/symbols/dwarf/dwarf.hpp +++ b/src/symbols/dwarf/dwarf.hpp @@ -66,8 +66,12 @@ namespace libdwarf { } ~die_object() { + release(); + } + + void release() { if(die) { - dwarf_dealloc_die(die); + dwarf_dealloc_die(exchange(die, nullptr)); } } @@ -75,16 +79,15 @@ namespace libdwarf { die_object& operator=(const die_object&) = delete; - die_object(die_object&& other) noexcept : dbg(other.dbg), die(other.die) { - // done for finding mistakes, attempts to use the die_object after this should segfault - // a valid use otherwise would be moved_from.get_sibling() which would get the next CU - other.dbg = nullptr; - other.die = nullptr; - } + // dbg doesn't strictly have to be st to null but it helps ensure attempts to use the die_object after this to + // segfault. A valid use otherwise would be moved_from.get_sibling() which would get the next CU. + die_object(die_object&& other) noexcept + : dbg(exchange(other.dbg, nullptr)), die(exchange(other.die, nullptr)) {} die_object& operator=(die_object&& other) noexcept { - std::swap(dbg, other.dbg); - std::swap(die, other.die); + release(); + dbg = exchange(other.dbg, nullptr); + die = exchange(other.die, nullptr); return *this; } diff --git a/src/symbols/dwarf/dwarf_utils.hpp b/src/symbols/dwarf/dwarf_utils.hpp index 2f8101c..56a46e5 100644 --- a/src/symbols/dwarf/dwarf_utils.hpp +++ b/src/symbols/dwarf/dwarf_utils.hpp @@ -24,12 +24,16 @@ namespace libdwarf { } } ~srcfiles() { + release(); + } + void release() { if(dw_srcfiles) { for(unsigned i = 0; i < dw_filecount; i++) { dwarf_dealloc(dbg, dw_srcfiles[i], DW_DLA_STRING); dw_srcfiles[i] = nullptr; } dwarf_dealloc(dbg, dw_srcfiles, DW_DLA_LIST); + dw_srcfiles = nullptr; } } srcfiles(const srcfiles&) = delete; @@ -38,9 +42,10 @@ namespace libdwarf { } srcfiles& operator=(const srcfiles&) = delete; srcfiles& operator=(srcfiles&& other) { - std::swap(dbg, other.dbg); - std::swap(dw_srcfiles, other.dw_srcfiles); - std::swap(dw_filecount, other.dw_filecount); + release(); + dbg = exchange(other.dbg, nullptr); + dw_srcfiles = exchange(other.dw_srcfiles, nullptr); + dw_filecount = exchange(other.dw_filecount, 0); return *this; } // note: dwarf uses 1-indexing @@ -177,7 +182,11 @@ namespace libdwarf { std::vector&& line_entries ) : version(version), line_context(line_context), line_entries(std::move(line_entries)) {} ~line_table_info() { + release(); + } + void release() { dwarf_srclines_dealloc_b(line_context); + line_context = nullptr; } line_table_info(const line_table_info&) = delete; line_table_info(line_table_info&& other) { @@ -185,9 +194,10 @@ namespace libdwarf { } line_table_info& operator=(const line_table_info&) = delete; line_table_info& operator=(line_table_info&& other) { - std::swap(version, other.version); - std::swap(line_context, other.line_context); - std::swap(line_entries, other.line_entries); + release(); + version = other.version; + line_context = exchange(other.line_context, nullptr); + line_entries = std::move(other.line_entries); return *this; } };