Cache and clean up allocated data for created DW_AT_data_member_location
diff --git a/dwflpp.cxx b/dwflpp.cxx
index 547ce50..b586701 100644
--- a/dwflpp.cxx
+++ b/dwflpp.cxx
@@ -124,6 +124,7 @@
   delete_map(cu_lines_cache);
 
   delete_map(cu_entry_pc_cache);
+  delete_map(dw_at_member_location_cache);
 
   if (dwfl)
     dwfl_end(dwfl);
@@ -3485,8 +3486,6 @@
     {
       /* dwarf_getlocation_addr doesn't understand DW_AT_data_bit offset,
        * so generate an equivalent DW_AT_data_member_location attr.
-       * FIXME:
-       *   properly clean up the allocated memory from mallocs
        */
       Dwarf_Attribute attr_mem;
       Dwarf_Die typedie;
@@ -3500,11 +3499,17 @@
 
       member_location = data_bit_to_byte_offset(byte_size, bit_offset);
       if (sess.verbose > 2)
-      clog << _F("member_location=%lu, bit_offset=%lu, byte_size=%lu\n",
+	clog << _F("member_location=%lu, bit_offset=%lu, byte_size=%lu\n",
 		 member_location, bit_offset, byte_size);
-      unsigned char *loc = (unsigned char *) malloc(8);
-      assert(loc);
-      write_uleb128(loc, member_location);
+      unsigned char *loc;
+      if (dw_at_member_location_cache.find(member_location) != dw_at_member_location_cache.end()) {
+	      loc = dw_at_member_location_cache[member_location];
+      }else {
+	      loc = new unsigned char[16];
+	      dw_at_member_location_cache[member_location] = loc;
+	      // write_uleb128 modifies loc, so do after the cache assignment
+	      write_uleb128(loc, member_location);
+      }
 
       Dwarf_Attribute data_member_location = {
 	      .code = DW_AT_data_member_location, .form = DW_FORM_udata,
diff --git a/dwflpp.h b/dwflpp.h
index 2249664..9017dd5 100644
--- a/dwflpp.h
+++ b/dwflpp.h
@@ -119,6 +119,9 @@
 typedef std::unordered_set<Dwarf_Addr> entry_pc_cache_t;
 typedef std::unordered_map<void*, entry_pc_cache_t*> cu_entry_pc_cache_t;
 
+// cache DW_AT_data_bit_offset converted to DW_AT_data_member_location
+typedef std::unordered_map<Dwarf_Word, unsigned char *> dw_at_member_location_cache_t;
+
 typedef std::vector<base_func_info> base_func_info_map_t;
 typedef std::vector<func_info> func_info_map_t;
 typedef std::vector<inline_instance_info> inline_instance_map_t;
@@ -572,6 +575,9 @@
   cu_entry_pc_cache_t cu_entry_pc_cache;
   bool check_cu_entry_pc(Dwarf_Die *cu, Dwarf_Addr pc);
 
+  // Cache for all the sythesized DW_AT_data_member_location attributes
+  dw_at_member_location_cache_t dw_at_member_location_cache;
+
   Dwarf_Die* get_parent_scope(Dwarf_Die* die);
 
   /* The global alias cache is used to resolve any DIE found in a