std::strstreambuf::~strstreambuf
From cppreference.com
< cpp | io | strstreambuf
C++
Input/output library
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
std::strstreambuf
| Public member functions | ||||
strstreambuf::~strstreambuf | ||||
| Protected member functions | ||||
| virtual ~strstreambuf(); |
(deprecated in C++98) (removed in C++26) |
|
Destroys a std::strstreambuf object. if the object is managing a dynamically-allocated buffer (the buffer state is "allocated") and if the object is not frozen, then deallocates the buffer using the deallocation function provided at construction or delete[] if none was provided.
[edit] Parameters
(none)
[edit] Notes
This destructor is typically called by the destructor of std::strstream.
If str() was called on a dynamic strstream and freeze(false) was not called after that, this destructor leaks memory.
[edit] Example
Run this code
#include <iostream> #include <strstream> void* my_alloc(size_t n) { std::cout << "my_alloc(" << n << ") called\n"; return new char[n]; } void my_free(void* p) { std::cout << "my_free() called\n"; delete[] (char*)p; } int main() { { std::strstreambuf buf(my_alloc, my_free); std::ostream s(&buf); s << 1.23 << std::ends; std::cout << buf.str() << '\n'; buf.freeze(false); } / destructor called here, buffer deallocated { std::strstreambuf buf(my_alloc, my_free); std::ostream s(&buf); s << 1.23 << std::ends; std::cout << buf.str() << '\n'; / buf.freeze(false); } / destructor called here, memory leak! }
Output:
my_alloc(4096) called 1.23 my_free() called my_alloc(4096) called 1.23