Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Dynamic linking on MacOSX & bus error
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-12-21 (15:15)
From: Francois Rouaix <francois.rouaix@g...>
Subject: Dynamic linking on MacOSX & bus error
This is a very MacOSX specific question I'm afraid - apologies to
platform-independent readers.
I'm perplexed by the following: the same simple code invoking an external
trivial Cocoa method works fine when linked with -custom and crashes when
not. Anyone has a clue of what I am doing wrong? This is ocaml 3.09.3 and a
G4 PB under 10.4.11 (Tiger).

$ cat foo.m
#include <caml/callback.h>
#include <caml/memory.h>

#import <Foundation/NSAutoreleasePool.h>

static NSAutoreleasePool *arp =nil;

value caml_init_default(value unit)
  if (nil == arp) {
    arp = [[NSAutoreleasePool alloc] init];

$ cat
external init : unit -> unit = "caml_init_default"
let () = init()
let f() =
  prerr_endline "Hello World!"

$ cat
let _ = Foo.f()

$ cat build
rm -f foo.o**
gcc -c -g -I$(ocamlc -where) foo.m
ocamlmklib -o foo foo.o -ccopt -g -ccopt -framework -ccopt Foundation
ocamlc -c
ocamlc -verbose -custom -o main-custom -g -I . foo.cma main.cmo
ocamlc -verbose -o main-dyn -g -I . foo.cma main.cmo

$ bash build
+ gcc  -o 'main-custom' -I'/usr/local/lib/ocaml' -g -framework Foundation
/tmp/camlprim5918c8.c  '-L.' '-L/usr/local/lib/ocaml' '-lfoo' '-lobjc'
-lcamlrun -lcurses -lpthread

$ ./main-custom
Hello World!

$ ./main-dyn
Bus error

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000020
0x90a410f8 in objc_msgSend ()
(gdb) bt
#0  0x90a410f8 in objc_msgSend ()
#1  0x00093f1c in caml_init_default (unit=1) at foo.m:13
#2  0x00018554 in caml_interprete ()
#3  0x000123fc in caml_main ()
#4  0x00002684 in main ()

Somehow NSautoreleasePool is nil?