mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2026-10-08 08:10:26 -04:00
2.9 KiB
2.9 KiB
Plan: Replace Raw new/delete with Smart Pointers
See also: RECOMMENDATIONS.md | PLAN.md | Threading Model
Current State
In src/CubicSDR.cpp:
- Multiple raw
new std::thread(...)andnew WorkerThread()allocations - Multiple raw
deleteoperations for thread and worker objects - Confirmed memory leaks:
m_glContextAttributes— never deletedconfNamewxString — never freedmodPathwxString — never freedappframe— never explicitly deleted
t_SDREnumoverwritten without joining/deleting the old thread
Implementation Plan
Phase 1: Declare smart pointer members
In src/CubicSDR.h, change declarations from:
std::thread *t_SDR;
SDRThread *sdrThread;
to:
std::unique_ptr<std::thread> t_SDR;
std::unique_ptr<SDRThread> sdrThread;
Apply to all thread and worker object pointers:
t_SDR,t_PostSDR,t_SpectrumVisual,t_DemodVisual,t_SDREnum,t_RigsdrThread,sdrPostThread,spectrumVisualThread,demodVisualThread,sdrEnum,rigThreadm_glContext(PrimaryGLContext*)
Phase 2: Update allocations
Replace all new calls with std::make_unique:
// Before:
t_SDR = new std::thread(&SDRThread::threadMain, sdrThread);
// After:
t_SDR = std::make_unique<std::thread>(&SDRThread::threadMain, sdrThread);
Phase 3: Remove all manual delete calls
Smart pointers handle cleanup automatically. Remove all 15 delete operations.
Phase 4: Fix memory leaks
m_glContextAttributes: wrap instd::unique_ptr<wxGLContextAttrs>or delete inOnExit().confName/modPath(lines 562, 579): change from rawnew wxStringto stack-allocatedwxStringorstd::unique_ptr.
Phase 5: Fix t_SDREnum overwrite
Before overwriting t_SDREnum, join the existing thread:
if (t_SDREnum && t_SDREnum->joinable()) {
t_SDREnum->join();
}
t_SDREnum = std::make_unique<std::thread>(...);
Verification Criteria
- Zero raw
new std::thread(...)ornew WorkerThread(...)allocations remain inCubicSDR.cpp. - Zero manual
deletecalls for thread or worker objects remain. - No memory leaks detected under repeated startup/shutdown cycles (verify with AddressSanitizer or Valgrind if available).
- All 4 confirmed memory leaks are fixed.
t_SDREnumoverwrite is guarded by a join check.- Build succeeds and application starts/closes cleanly.
Rollback Strategy
If smart pointer changes cause crashes (e.g., a thread joined twice, or a use-after-free):
git revertthe commit.- The original raw
new/deletecode is functional — the revert is safe. - If only specific phases cause issues, revert to the last working phase (e.g., keep Phase 1 declarations but revert Phase 2 allocations).
Files to Modify
| File | Action |
|---|---|
src/CubicSDR.h |
Change pointer declarations to smart pointers |
src/CubicSDR.cpp |
Update allocations, remove deletes, fix leaks |