Convert the `free_register_cache` function into a destructor of the
regcache struct. In one place, we completely remove the call to free
the regcache object by stack-allocating the object.
We also delete the copy constructor explicitly to prevent the risk of
copying regcaches and then double-freeing buffers when they are
destructed.
---
gdbserver/gdbthread.h | 2 +-
gdbserver/regcache.cc | 15 +++++----------
gdbserver/regcache.h | 9 +++++----
gdbserver/server.cc | 8 +++-----
4 files changed, 14 insertions(+), 20 deletions(-)
@@ -35,7 +35,7 @@ struct thread_info
~thread_info ()
{
- free_register_cache (this->regcache_data);
+ delete this->regcache_data;
}
/* The id of this thread. */
@@ -165,16 +165,11 @@ regcache::regcache (const target_desc *tdesc)
initialize (tdesc, nullptr);
}
-void
-free_register_cache (struct regcache *regcache)
+regcache::~regcache ()
{
- if (regcache)
- {
- if (regcache->registers_owned)
- free (regcache->registers);
- free (regcache->register_status);
- delete regcache;
- }
+ if (registers_owned)
+ free (registers);
+ free (register_status);
}
#endif
@@ -280,7 +275,7 @@ free_register_cache_thread (struct thread_info *thread)
if (regcache != NULL)
{
regcache_invalidate_thread (thread);
- free_register_cache (regcache);
+ delete regcache;
set_thread_regcache_data (thread, NULL);
}
}
@@ -51,6 +51,11 @@ struct regcache : public reg_buffer_common
/* Constructors. */
regcache () = default;
regcache (const target_desc *tdesc);
+ regcache (const regcache &rhs) = delete;
+ regcache &operator= (const regcache &rhs) = delete;
+
+ /* Deconstructor. */
+ virtual ~regcache ();
#endif
/* Init the regcache data. */
@@ -77,10 +82,6 @@ struct regcache : public reg_buffer_common
regcache *get_thread_regcache (thread_info *thread, bool fetch = true);
-/* Release all memory associated with the register cache for INFERIOR. */
-
-void free_register_cache (struct regcache *regcache);
-
/* Invalidate cached registers for one thread. */
void regcache_invalidate_thread (struct thread_info *);
@@ -4237,15 +4237,13 @@ process_serial_event (void)
require_running_or_break (cs.own_buf);
if (cs.current_traceframe >= 0)
{
- struct regcache *regcache
- = new struct regcache (current_target_desc ());
+ regcache a_regcache (current_target_desc ());
if (fetch_traceframe_registers (cs.current_traceframe,
- regcache, -1) == 0)
- registers_to_string (regcache, cs.own_buf);
+ &a_regcache, -1) == 0)
+ registers_to_string (&a_regcache, cs.own_buf);
else
write_enn (cs.own_buf);
- free_register_cache (regcache);
}
else
{