Using onOutOfMemoryError in C wrappers

Jack jckj33 at gmail.com
Wed Mar 24 15:42:41 UTC 2021


On Wednesday, 24 March 2021 at 08:31:19 UTC, Basile B. wrote:
> On Wednesday, 24 March 2021 at 07:58:22 UTC, Per Nordlöw wrote:
>> When wrapping C code that tries to allocate memory resources 
>> via functions such as
>>
>>     X* X_create();
>>
>> should one call `onOutOfMemoryError();` upon null return?
>>
>> Making more D wrappers `nothrow @nogc`.
>
>
> There are several ways to do that. In addition to 
> onOutOfMemoryError, you can use a static instance or `if 
> (somePtr is null) assert(0);`
>
>   void v() @nogc nothrow
>   {
>     __gshared oom = new OutOfMemoryError();
>     auto X* = X_create();
>     if (X is null)
>         throw oom;
>   }

why are you creating oom variable before the if(x is null) check?


More information about the Digitalmars-d-learn mailing list