std::strstreambuf::~strstreambuf
提供: cppreference.com
<tbody>
</tbody>
virtual ~strstreambuf(); |
||
std::strstreambuf オブジェクトを破棄します。 オブジェクトが動的確保されたバッファを管理している (バッファ状態が「確保された」) 場合かつオブジェクトが凍結されていない場合は、構築時に提供された確保関数、または提供されなかった場合は delete[] を用いて、バッファを解放します。
引数
(なし)
ノート
このデストラクタは一般的には std::strstream のデストラクタによって呼ばれます。
動的な strstream に対して str() が呼ばれ、その後 freeze(false) が呼ばれていない場合、このデストラクタはメモリをリークします。
例
Run this code
#include <strstream>
#include <iostream>
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