Skip to content

Commit ecaa144

Browse files
authored
Fix a memory "leak" in VisMF's persistent streams (#3592)
## Summary VisMF uses a std::map to keep track of the streams it opens. Each PersistentIFStream object also has a vector used as IO buffer. When a stream is closed, we need to free to memory used by the buffer. The issue was it used std::vector::clear(), which only changes size() without reducing capacity() at all. So the memory was never freed. The issue is now fixed by std::vector::swap. ## Additional background Thank Simon Guichandut (@simonguichandut) for reporting this! Thank Eric Johnson (@yut23) for pinning down the location of the leak with the help of the Massif heap profiler! ## Checklist The proposed changes: - [x] fix a bug or incorrect behavior in AMReX - [ ] add new capabilities to AMReX - [ ] changes answers in the test suite to more than roundoff level - [ ] are likely to significantly affect the results of downstream AMReX users - [ ] include documentation in the code and/or rst files, if appropriate
1 parent 77d4d1f commit ecaa144

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

Src/Base/AMReX_VisMF.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ void VisMF::CloseStream(const std::string &fileName, bool forceClose)
22022202
pifs.pstr = nullptr;
22032203
pifs.isOpen = false;
22042204
}
2205-
pifs.ioBuffer.clear();
2205+
VisMFBuffer::ClearBuffer(pifs.ioBuffer);
22062206
}
22072207

22082208

Src/Base/AMReX_VisMFBuffer.H

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public:
2929
ioBufferSize = iobuffersize;
3030
}
3131

32+
static void ClearBuffer (IO_Buffer& buf) {
33+
IO_Buffer().swap(buf);
34+
}
35+
3236
protected:
3337

3438
static AMREX_EXPORT Long ioBufferSize; //!< ---- the settable buffer size

0 commit comments

Comments
 (0)