Override assert handler
kinke
noone at nowhere.com
Sat Aug 17 12:43:36 UTC 2024
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.
More information about the Digitalmars-d
mailing list