std::strstr...trstreambuf(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::strstreambuf::~strstreambuf πŸ’…

okay bestie, welcome to the <std::strstreambuf::~strstreambuf> page from the manual named C++ Standard Libary (that's the manual name, exactly β€” and the page identifier is std::strstr...trstreambuf(3)). We're talking about a destructor, and it is highkey deprecated-era lore β€” no cap. ✨

🌸 NAME 🌸

  • std::strstreambuf::~strstreambuf β€” std::strstreambuf::~strstreambuf (yes, the NAME section said it twice, and honestly? iconic.)

✨ Synopsis ✨

virtual ~strstreambuf();  (deprecated in C++98)
                          (removed in C++26)

so yeah, the signature is virtual ~strstreambuf();, and it carries two big labels: deprecated in C++98 and removed in C++26. πŸ’€

It destroys a std::strstreambuf object. If the object is managing a dynamically-allocated buffer (the buffer state is "allocated") and it is not frozen, then it deallocates the buffer using the deallocation function provided at construction β€” or delete[] if none was provided. That's the whole behavior, bestie. πŸ’–

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally no parameters. Destructor stays unbothered. πŸ’…

πŸ¦‹ Notes πŸ¦‹

This destructor is typically called by the destructor of std::strstream.

If str() was called on a dynamic strstream and freeze(false) was not called after that, this destructor leaks memory. 😭 Memory leak warning, bestie: do not ignore. So if that's your situation, please remember the freeze(false) after str() β€” leaking memory is not the move.

πŸ’œ Example πŸ’œ

// Run this code

#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!
}

Output:

my_alloc(4096) called
1.23
my_free() called
my_alloc(4096) called
1.23

okay that's the whole page, bestie. from http://cppreference.com, version/date 2024.06.10, page std::strstr...trstreambuf(3) β€” go forth and freeze your buffers appropriately πŸ«ΆπŸ’–

πŸ“¦ where this page comes from