std::strstreambuf::~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
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/io/strstreambuf/~strstreambuf