Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] segfault in caml_copy_string
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile Starynkevitch [local] <basile.starynkevitch@i...>
Subject: Re: [Caml-list] segfault in caml_copy_string
On Fri, Jul 16, 2004 at 04:10:47PM +0200, Bauer, Christoph wrote:
> Hi,
> 
> here is a simple C-function:
> 
> CAMLprim value getparam(value request, value key)
> {
>   CAMLparam2(request, key);
>   const char * result;
>   CAMLreturn(key);  /* 1 */
>   CAMLreturn(caml_copy_string("test")); /* 2 */
>  }
> 
> Version 1 returns the key and everything works (except that
> key is not what I need.)
> 
> Version 2 calls caml_copy_string and this fails. Has anybody a idea why?

caml_copy_string actually allocates a *garbage-collected* string, so
you need to tell about its result to the GC. Try instead

 CAMLprim value getparam(value request, value key){
   CAMLparam2(request, key);
   CAMLlocal1(result);
   result = caml_copy_string("test");
   CAML_return(result);
}

(to understand why, read any material on precise moving garbage
collectors)

-- 
Basile STARYNKEVITCH -- basile dot starynkevitch at inria dot fr
Project cristal.inria.fr - phone +33 1 3963 5197 - mobile 6 8501 2359
http://cristal.inria.fr/~starynke --- all opinions are only mine 

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners