std::strstreambuf:: ~strstreambuf
From cppreference.net
<
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
(
)
;
|
(C++98 中已弃用)
(C++26 中移除) |
|
销毁一个
std::strstreambuf
对象。如果该对象正在管理动态分配的缓冲区(缓冲区状态为"已分配")且对象未被冻结,则使用构造时提供的释放函数或
delete
[
]
(若未提供释放函数)来释放该缓冲区。
参数
(无)
注释
此析构函数通常由 std::strstream 的析构函数调用。
如果在动态
strstream
上调用过
str()
且之后未调用
freeze(false)
,则该析构函数会发生内存泄漏。
示例
运行此代码
#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); } // 析构函数在此处调用,缓冲区被释放 { std::strstreambuf buf(my_alloc, my_free); std::ostream s(&buf); s << 1.23 << std::ends; std::cout << buf.str() << '\n'; // buf.freeze(false); } // 析构函数在此处调用,内存泄漏! }
输出:
my_alloc(4096) called 1.23 my_free() called my_alloc(4096) called 1.23