D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15514 - Segfault when calling valid D code from C
Summary: Segfault when calling valid D code from C
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 blocker
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-01-04 13:27 UTC by Puneet Goel
Modified: 2022-10-28 09:12 UTC (History)
2 users (show)

See Also:


Attachments
D and C code files along with makefile (628 bytes, application/x-compressed-tar)
2016-01-04 13:27 UTC, Puneet Goel
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Puneet Goel 2016-01-04 13:27:07 UTC
Created attachment 1571 [details]
D and C code files along with makefile

Trying to call D code from C is resulting in segfault.
Comment 1 Puneet Goel 2016-01-04 13:28:13 UTC
(In reply to Puneet Goel from comment #0)
> Created attachment 1571 [details]
> D and C code files along with makefile
> 
> Trying to call D code from C is resulting in segfault.

Kindly see the attachment to look at the code that segfaults.
Comment 2 ag0aep6g 2016-01-04 21:15:01 UTC
Reduced the test case a little:

main.c:
----
extern void initialize();

int main(int argc, char*argv[]) {
  initialize();
  return 0;
}
----

foo.d:
----
extern(C) void initialize() {
  import core.runtime;  
  Runtime.initialize;
  int x;
  auto dg = {x = 2;};
  Runtime.terminate();
}
----

Compiling and running:
----
$ gcc -c main.c
$ dmd foo.d main.o
$ ./foo
Segmentation fault (core dumped)
----
Comment 3 ag0aep6g 2016-01-04 21:19:25 UTC
I think the problem is that the memory for the closure is allocated as soon as the initialize function is entered, i.e. before the call to Runtime.initialize.
Comment 4 RazvanN 2022-10-28 09:12:37 UTC
I cannot reproduce this example. Compiling the reduced test case and running it now leads to graceful program exit.