[LLVMbugs] [Bug 22021] libc++ std::ostrstream leaks memory.

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Wed Dec 24 11:31:22 PST 2014


http://llvm.org/bugs/show_bug.cgi?id=22021

Marshall Clow (home) <mclow.lists at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Marshall Clow (home) <mclow.lists at gmail.com> ---
This is a "required" memory leak (if you can believe that).

When you call strstreambuf::str() (which ostrstream is derived from), you get a
pointer to its internal buffer, and the stream is "frozen" (see
depr.strstreambuf.members/2 in the standard). Note that in spite of the method
being named "str", it does NOT return a std::string.

When the stream is destructed, the buffer may not be freed if the stream is
"frozen".
(see depr.strstreambuf.cons/8). 

If you rewrite your code thus:

    static std::string MessageComposer( uint32_t index )
    {
    std::ostrstream stm;
    stm << "This is message #" << index << "." << std::endl;
    std::string foo = stm.str();
    stm.freeze(false);
    return foo;
    }

you will see that the "leak" has disappeared.
(Yeah, it's easy to misuse this object. Maybe that's why it is deprecated)

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20141224/6fe474a9/attachment.html>


More information about the llvm-bugs mailing list