Describe the bug
Summary
get_exx_potential and get_exx_stress_potential in source/source_pw/module_pwdft/op_pw_exx_pot.cpp allocate a CPU buffer (pot_cpu = new Real[npw]) but can return early or throw before the matching delete[], leaking the allocation.
Affected locations
| Function |
new line |
Leaking exit |
delete[] line |
get_exx_potential |
37 |
return at line 52 |
225 |
get_exx_potential |
37 |
throw at line 213 |
225 |
get_exx_stress_potential |
250 |
throw at line 403 |
415 |
- Early return:
if (ik > nks) { return; } at line 52 leaks pot_cpu every time it triggers.
- GPU exception path: If
cudaHostRegister fails, the throw at lines 213 / 403 leaks pot_cpu and also leaves the pointer registered.
Impact
- Leak size:
sizeof(Real) * npw bytes per call (typically ~100 KB–1 MB).
- Trigger frequency: Any EXX calculation where
ik > nks (e.g., certain k-parallel or restart paths). If this happens once per SCF step per k-point, the leak is small but unbounded over long runs.
Suggested fix
Move the allocation after the early-return guard, or wrap pot_cpu in a std::unique_ptr<Real[]>:
### Expected behavior
_No response_
### To Reproduce
_No response_
### Environment
_No response_
### Additional Context
_No response_
### Task list for Issue attackers (only for developers)
- [ ] Verify the issue is not a duplicate.
- [ ] Describe the bug.
- [ ] Steps to reproduce.
- [ ] Expected behavior.
- [ ] Error message.
- [ ] Environment details.
- [ ] Additional context.
- [ ] Assign a priority level (low, medium, high, urgent).
- [ ] Assign the issue to a team member.
- [ ] Label the issue with relevant tags.
- [ ] Identify possible related issues.
- [ ] Create a unit test or automated test to reproduce the bug (if applicable).
- [ ] Fix the bug.
- [ ] Test the fix.
- [ ] Update documentation (if necessary).
- [ ] Close the issue and inform the reporter (if applicable).
Describe the bug
Summary
get_exx_potentialandget_exx_stress_potentialinsource/source_pw/module_pwdft/op_pw_exx_pot.cppallocate a CPU buffer (pot_cpu = new Real[npw]) but can return early or throw before the matchingdelete[], leaking the allocation.Affected locations
newlinedelete[]lineget_exx_potentialreturnat line 52get_exx_potentialthrowat line 213get_exx_stress_potentialthrowat line 403if (ik > nks) { return; }at line 52 leakspot_cpuevery time it triggers.cudaHostRegisterfails, thethrowat lines 213 / 403 leakspot_cpuand also leaves the pointer registered.Impact
sizeof(Real) * npwbytes per call (typically ~100 KB–1 MB).ik > nks(e.g., certain k-parallel or restart paths). If this happens once per SCF step per k-point, the leak is small but unbounded over long runs.Suggested fix
Move the allocation after the early-return guard, or wrap
pot_cpuin astd::unique_ptr<Real[]>: