caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Dynamic linking on MacOSX & bus error
@ 2007-12-21 15:15 Francois Rouaix
  2007-12-23  2:49 ` Francois Rouaix
  0 siblings, 1 reply; 2+ messages in thread
From: Francois Rouaix @ 2007-12-21 15:15 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1744 bytes --]

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
#define CAML_NAME_SPACE
#include <caml/callback.h>
#include <caml/memory.h>

#import <Foundation/NSAutoreleasePool.h>

static NSAutoreleasePool *arp =nil;

value caml_init_default(value unit)
{
  CAMLparam0();
  if (nil == arp) {
    arp = [[NSAutoreleasePool alloc] init];
  }
  CAMLreturn(Val_int(0));
}

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

$ cat main.ml
let _ = Foo.f()

$ cat build
rm -f foo.o foo.cm* main.cm*
gcc -c -g -I$(ocamlc -where) foo.m
ocamlmklib -o foo foo.ml foo.o -ccopt -g -ccopt -framework -ccopt Foundation
-lobjc
ocamlc -c main.ml
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?
--f

[-- Attachment #2: Type: text/html, Size: 3291 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Dynamic linking on MacOSX & bus error
  2007-12-21 15:15 Dynamic linking on MacOSX & bus error Francois Rouaix
@ 2007-12-23  2:49 ` Francois Rouaix
  0 siblings, 0 replies; 2+ messages in thread
From: Francois Rouaix @ 2007-12-23  2:49 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2127 bytes --]

Replying to myself: RTFM.ocamlmklib actually has a "-framework foo" option
for MacOSX, and it works. What I was doing instead "-ccopt -framework -ccopt
foo" doesn't work.
Solved.


On Dec 21, 2007 7:15 AM, Francois Rouaix <francois.rouaix@gmail.com> wrote:

> 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
> #define CAML_NAME_SPACE
> #include <caml/callback.h>
> #include <caml/memory.h>
>
> #import <Foundation/NSAutoreleasePool.h>
>
> static NSAutoreleasePool *arp =nil;
>
> value caml_init_default(value unit)
> {
>   CAMLparam0();
>   if (nil == arp) {
>     arp = [[NSAutoreleasePool alloc] init];
>   }
>   CAMLreturn(Val_int(0));
> }
>
> $ cat foo.ml
> external init : unit -> unit = "caml_init_default"
> let () = init()
> let f() =
>   prerr_endline "Hello World!"
>
> $ cat main.ml
> let _ = Foo.f()
>
> $ cat build
> rm -f foo.o foo.cm* main.cm*
> gcc -c -g -I$(ocamlc -where) foo.m
> ocamlmklib -o foo foo.ml foo.o -ccopt -g -ccopt -framework -ccopt
> Foundation -lobjc
> ocamlc -c main.ml
> 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?
> --f
>
>

[-- Attachment #2: Type: text/html, Size: 3467 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-12-23  2:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-21 15:15 Dynamic linking on MacOSX & bus error Francois Rouaix
2007-12-23  2:49 ` Francois Rouaix

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).