C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Deliberate memory leak

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 14 Oct 2024 11:22:20 +0100
On Mon, Oct 14, 2024 at 12:07 AM Richard Hodges wrote:
>
> MyThing* mything_ptr = new ( sbrk(sizeof(MyThing)) ) MyThing ();
>
> voila. Your MyThing will never be destroyed, and malloc/new will be completely unaware of it.
>
> No need to alter the standard.


But what if the allocation takes place inside a proprietary shared
library? Here's a patch I wrote for libpango to clean up the output
from valgrind:

    https://github.com/healytpk/libpango/commit/629b67918d7d2de6060e3bfc6246c3d6e76c15a6

An alternative to editing the source code for libpango would have been
to do the following in my own program:

    SomeClass *p = pango_some_function( a, b, c ):
    for ( unsigned i = 0u; i < p->fonts->count; ++i )
std::deliberate_leak( p->fonts->f[i] );

std::deliberate_leak would be most useful in cases where you don't
have the library's source code.

Received on 2024-10-14 10:22:28