-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extern(C) functions should be nothrow @nogc
#36
Comments
Would this include any C function that takes a function pointer, as it could be passed a D function (marked with extern(C)) that allocates or throws? |
@rcorre you can mark functions/delegates as nothrow. alias myFunc = extern(C) void function() nothrow; Any function being passed to or is a c function declaration should really be |
So marking a such a C function as |
There are two separate cases going on here: extern(C)
void cFunc() nothrow @system @nogc; alias ACFunc = extern(C) void function() nothrow @system @nogc;
extern(C)
void cFunc(ACFunc arg) nothrow @system @nogc; So in the first case is the same as defining a function in C and exporting it for access via shared library loading, but it won't accept any arguments that are functions. In the second case we pass in a function to a C function. Keep in mind both the C function as well as the argument must be declared as being After all, our exceptions and GC collecting won't work from within C code and good chance it'll end in a segfault. The only attribute that can be not included in this is @nogc as long as you know what the C code is doing and in most cases its fine. |
I've experienced such segfaults first hand some time ago with GtkD. My D code threw an exception inside C code and druntime tried to unwind the stack. To do so it looks at the frame pointer register, but all my C libraries are compiled without explicit frame pointer. So druntime misinterpreted the register for a stack frame and crashed when dereferencing it. |
Resolves SiegeLord#36: Extern(C) functions should be `nothrow @nogc`
Resolves SiegeLord#36: Extern(C) functions should be `nothrow @nogc`
If they don't call D code internally, C functions should be marked
extern(C) nothrow @nogc { … }
.The text was updated successfully, but these errors were encountered: