Override assert handler

Manu turkeyman at gmail.com
Sun Aug 18 04:50:09 UTC 2024


On Sat, 17 Aug 2024 at 22:46, kinke via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> On Saturday, 17 August 2024 at 09:31:53 UTC, Manu wrote:
> > I mean, there's also the `assertHandler()` stuff, but I don't
> > really see the point; I'd rather just replace the assert
> > handler symbol directly. Using `assertHandler` is awkward
> > because you need a static constructor to register it at
> > runtime, and then anything you do in your assert handler almost
> > inevitably leads to bootup issues with cyclic module
> > dependencies because everything leads back to assert.
>
> You can set the handler in a CRT constructor, which avoids any
> cycles and makes sure it is installed before initializing
> druntime (`rt_init()`), so should really cover all asserts:
> ```
> pragma(crt_constructor)
> void setupAssertHandler()
> {
>      import core.exception : assertHandler;
>      assertHandler = &myAssertHandler;
> }
>
> void myAssertHandler(string file, size_t line, string msg) nothrow
> {
>      // print...
>      import core.stdc.stdlib;
>      abort(); // or something like that
> }
> ```
>
> `@core.attribute.weak` isn't implemented by DMD, plus needs
> emulation on Windows (done by LDC) with according limitations.
> The extra indirection via that custom assert handler in druntime
> works e.g. for a pre-linked druntime DLL too.
>

That's a reasonable solution. Thanks for the tip!
I'd still prefer to just replace a weak library call though, so that calls
to assert are direct and don't add a bunch of extra layers to the callstack.

I'd also really like it if the condition were stringified and handed to the
assert handler... that seems like a weird oversight?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20240818/8a04ac88/attachment.htm>


More information about the Digitalmars-d mailing list