Skip to content

Commit e75c08e

Browse files
author
marius
committed
fix: resolve memory leak in CPU destructors
Fix double-delete bug where m_qk was allocated in base class CPU constructor but deleted in derived class destructors (CPURV32/CPURV64). Changes: - Move m_qk deletion from derived class destructors to base class CPU destructor where it was originally allocated - Add null pointer checks before all delete operations in destructors - Set pointers to nullptr after deletion for safety - Add comments explaining cleanup responsibility This fixes: - Double-delete bug that could cause undefined behavior - Potential memory leaks from improper cleanup order - Missing null pointer safety checks Files modified: - inc/CPU.h: Changed base class destructor from =default to proper implementation - src/CPU.cpp: Added base class destructor to delete m_qk - src/RV32.cpp: Removed m_qk deletion, added null checks - src/RV64.cpp: Removed m_qk deletion, added null checks
1 parent 16d31f1 commit e75c08e

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

inc/CPU.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace riscv_tlm {
4242
CPU& operator=(CPU&& other) noexcept = delete;
4343

4444
/* Destructors */
45-
~CPU() override = default;
45+
~CPU() override;
4646

4747
/**
4848
* @brief Perform one instruction step

src/CPU.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ namespace riscv_tlm {
4444
dmi_ptr_valid = false;
4545
}
4646

47+
CPU::~CPU() {
48+
if (m_qk) {
49+
delete m_qk;
50+
m_qk = nullptr;
51+
}
52+
}
53+
4754
[[noreturn]] void CPU::CPU_thread() {
4855

4956
while (true) {

src/RV32.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,31 @@ namespace riscv_tlm {
3636
}
3737

3838
CPURV32::~CPURV32() {
39-
delete register_bank;
40-
delete mem_intf;
41-
delete base_inst;
42-
delete c_inst;
43-
delete m_inst;
44-
delete a_inst;
45-
delete m_qk;
39+
if (register_bank) {
40+
delete register_bank;
41+
register_bank = nullptr;
42+
}
43+
if (mem_intf) {
44+
delete mem_intf;
45+
mem_intf = nullptr;
46+
}
47+
if (base_inst) {
48+
delete base_inst;
49+
base_inst = nullptr;
50+
}
51+
if (c_inst) {
52+
delete c_inst;
53+
c_inst = nullptr;
54+
}
55+
if (m_inst) {
56+
delete m_inst;
57+
m_inst = nullptr;
58+
}
59+
if (a_inst) {
60+
delete a_inst;
61+
a_inst = nullptr;
62+
}
63+
// m_qk is handled by base class destructor
4664
}
4765

4866
bool CPURV32::cpu_process_IRQ() {

src/RV64.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,31 @@ namespace riscv_tlm {
3434
}
3535

3636
CPURV64::~CPURV64() {
37-
delete register_bank;
38-
delete mem_intf;
39-
delete base_inst;
40-
delete c_inst;
41-
delete m_inst;
42-
delete a_inst;
43-
delete m_qk;
37+
if (register_bank) {
38+
delete register_bank;
39+
register_bank = nullptr;
40+
}
41+
if (mem_intf) {
42+
delete mem_intf;
43+
mem_intf = nullptr;
44+
}
45+
if (base_inst) {
46+
delete base_inst;
47+
base_inst = nullptr;
48+
}
49+
if (c_inst) {
50+
delete c_inst;
51+
c_inst = nullptr;
52+
}
53+
if (m_inst) {
54+
delete m_inst;
55+
m_inst = nullptr;
56+
}
57+
if (a_inst) {
58+
delete a_inst;
59+
a_inst = nullptr;
60+
}
61+
// m_qk is handled by base class destructor
4462
}
4563

4664
bool CPURV64::cpu_process_IRQ() {

0 commit comments

Comments
 (0)