Namespaces
Variants

std::strstreambuf:: ~strstreambuf

From cppreference.net
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