Managing malloced memory
jfondren
julian.fondren at gmail.com
Mon Oct 11 12:20:27 UTC 2021
On Monday, 11 October 2021 at 12:09:07 UTC, Imperatorn wrote:
> On Wednesday, 6 October 2021 at 18:06:38 UTC, anon wrote:
>> I interface to a C library that gives me a malloced object.
>> How can I manage that pointer so that it gets freed
>> automatically.
>> What I've thought of so far:
>> * scope(exit): not an option because I want to return that
>> memory
>
> Explain again why scope exit isn't an option
The explanation is "I want to return that memory".
```d
int* not_an_option() {
import core.memory : pureMalloc, pureFree;
int* p = cast(int*) pureMalloc(int.sizeof);
scope (exit)
pureFree(p);
return p;
}
unittest {
not_an_option()[0] = 1;
}
```
valgrind: Invalid write of size 4
More information about the Digitalmars-d-learn
mailing list