@@ -366,12 +366,19 @@ Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) {
366366 if (--it->second .ref_count > 0 )
367367 return Status ();
368368
369+ // Remove the entry from m_software_breakpoints rightaway, so that we don't
370+ // leave behind an entry with ref_count == 0 in case one of the following
371+ // conditions returns an error. The breakpoint is moved so that it can be
372+ // accessed below.
373+ SoftwareBreakpoint bkpt = std::move (it->second );
374+ m_software_breakpoints.erase (it);
375+
369376 // This is the last reference. Let's remove the breakpoint.
370377 Status error;
371378
372379 // Clear a software breakpoint instruction
373- llvm::SmallVector<uint8_t , 4 > curr_break_op (
374- it-> second . breakpoint_opcodes . size (), 0 );
380+ llvm::SmallVector<uint8_t , 4 > curr_break_op (bkpt. breakpoint_opcodes . size (),
381+ 0 );
375382
376383 // Read the breakpoint opcode
377384 size_t bytes_read = 0 ;
@@ -382,10 +389,10 @@ Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) {
382389 " addr=0x%" PRIx64 " : tried to read %zu bytes but only read %zu" , addr,
383390 curr_break_op.size (), bytes_read);
384391 }
385- const auto &saved = it-> second .saved_opcodes ;
392+ const auto &saved = bkpt .saved_opcodes ;
386393 // Make sure the breakpoint opcode exists at this address
387- if (llvm::ArrayRef (curr_break_op) != it-> second .breakpoint_opcodes ) {
388- if (curr_break_op != it-> second .saved_opcodes )
394+ if (llvm::ArrayRef (curr_break_op) != bkpt .breakpoint_opcodes ) {
395+ if (curr_break_op != bkpt .saved_opcodes )
389396 return Status::FromErrorString (
390397 " Original breakpoint trap is no longer in memory." );
391398 LLDB_LOG (log,
@@ -418,7 +425,6 @@ Status NativeProcessProtocol::RemoveSoftwareBreakpoint(lldb::addr_t addr) {
418425 llvm::make_range (saved.begin (), saved.end ()));
419426 }
420427
421- m_software_breakpoints.erase (it);
422428 return Status ();
423429}
424430
0 commit comments