Skip to content

Memory leak in OperatorEXXPW when ik > nks triggers early return #8022

Description

@mohanchen

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).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugsBugs that only solvable with sufficient knowledge of DFTEXX and lr-TDDFTRelated to EXX or lr-TDDFT

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions