Your C code doesn't cooperate with the "3m" garbage collector, which is
the way Racket is built by default. For example, the object that `env`
references might be moved by the GC without the `env` variable being
updated.

See

 http://docs.racket-lang.org/inside/embedding.html#%28part._3m_.Embedding%29

for more information.



At Mon, 29 Feb 2016 18:01:11 -0800 (PST), Felipe Oliveira Carvalho wrote:
> I'm trying to embed Racket into a test C program (minimal.c). I can 
> successfully compile it with clang on Mac OS X, but the program segfaults 
> when 
> I try to run it. Any idea on what I'm doing wrong?
> 
> 
> 
> #include <stdio.h>
> #include <scheme.h>
> 
> #include "base.c"
> 
> static int our_scheme_main(Scheme_Env *env, int argc, char *argv[]) {
>   puts("our_scheme_main:");
> 
>   declare_modules(env);
>   scheme_namespace_require(scheme_intern_symbol("racket/base"));
> 
>   scheme_eval_string("(+ 2 1)", env);
> 
>   return 0;
> }
> 
> int main(int argc, char *argv[]) {
>   int status = scheme_main_setup(1, our_scheme_main, argc, argv);
> 
>   return status;
> }
> 
> 
> 
> 
> 
> felipe@Felipes-MacBook-Pro:~/code/embed_racket (master #%)$ make
> clang -I "/Users/felipe/Applications/Racket v6.4"/include/ --std=gnu99 -g -F 
> "/Users/felipe/Applications/Racket v6.4"/lib/ -framework Racket minimal.c
> 
> 
> felipe@Felipes-MacBook-Pro:~/code/embed_racket (master #%)$ ./a.out
> our_scheme_main:
> Segmentation fault: 11
> 
> 
> felipe@Felipes-MacBook-Pro:~/code/embed_racket (master #%)$ lldb ./a.out
> (lldb) target create "./a.out"
> Current executable set to './a.out' (x86_64).
> (lldb) r
> Process 8849 launched: './a.out' (x86_64)
> our_scheme_main:
> Process 8849 stopped
> * thread #1: tid = 0x3eb99, 0x0000000100400b99 Racket`do_scope_at_phase + 
> 249, 
> queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, 
> address=0x0)
>     frame #0: 0x0000000100400b99 Racket`do_scope_at_phase + 249
> Racket`do_scope_at_phase:
> ->  0x100400b99 <+249>: movzwl (%rsi), %eax
>     0x100400b9c <+252>: cmpl   $0x4f, %eax
>     0x100400b9f <+255>: jne    0x100400cac               ; <+524>
>     0x100400ba5 <+261>: testb  $0x1, 0x2(%rsi)
> (lldb) thread backtrace
> * thread #1: tid = 0x3eb99, 0x0000000100400b99 Racket`do_scope_at_phase + 
> 249, 
> queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, 
> address=0x0)
>   * frame #0: 0x0000000100400b99 Racket`do_scope_at_phase + 249
>     frame #1: 0x00000001003e6d39 Racket`stx_adjust_scope + 345
>     frame #2: 0x00000001003efbc1 Racket`scheme_stx_push_module_context + 161
>     frame #3: 0x000000010013cc9c Racket`scheme_top_introduce + 348
>     frame #4: 0x0000000100143b8e Racket`compile_k + 670
>     frame #5: 0x0000000100166f66 Racket`scheme_top_level_do_worker + 2102
>     frame #6: 0x000000010013cfbc Racket`scheme_eval + 220
>     frame #7: 0x000000010013ed53 Racket`do_eval_string_all + 627
>     frame #8: 0x0000000100000e96 
> a.out`our_scheme_main(env=0x00000001026897e8, 
> argc=1, argv=0x00007fff5fbff8e8) + 86 at minimal.c:12
>     frame #9: 0x00000001000d8323 Racket`call_with_basic + 147
>     frame #10: 0x00000001000d824a Racket`scheme_main_stack_setup + 330
>     frame #11: 0x00000001000d80f3 Racket`scheme_main_setup + 35
>     frame #12: 0x0000000100000e2b a.out`main(argc=1, argv=0x00007fff5fbff8e8) 
> + 59 at minimal.c:18
>     frame #13: 0x00007fff96bbd5ad libdyld.dylib`start + 1
>     frame #14: 0x00007fff96bbd5ad libdyld.dylib`start + 1
> (lldb)
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to