std::strstreambuf::~strstreambuf
來自 cppreference.com
< cpp | io | strstreambuf
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); } // 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! }
輸出
my_alloc(4096) called 1.23 my_free() called my_alloc(4096) called 1.23