名前空間
変種
操作

std::strstreambuf::~strstreambuf

提供: cppreference.com
< cpp‎ | io‎ | strstreambuf
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
 
virtual ~strstreambuf();

std::strstreambuf オブジェクトを破棄します。 オブジェクトが動的確保されたバッファを管理している (バッファ状態が「確保された」) 場合かつオブジェクトが凍結されていない場合は、構築時に提供された確保関数、または提供されなかった場合は delete[] を用いて、バッファを解放します。

[編集] 引数

(なし)

[編集] ノート

このデストラクタは一般的には std::strstream のデストラクタによって呼ばれます。

動的な strstream に対して str() が呼ばれ、その後 freeze(false) が呼ばれていない場合、このデストラクタはメモリをリークします。

[編集]

#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