caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Toplevel crashes when trying to call external functions
@ 2004-07-12  3:39 John Prevost
  2004-07-12  4:46 ` Andy Yang
  2004-07-12 10:55 ` Gerd Stolpmann
  0 siblings, 2 replies; 14+ messages in thread
From: John Prevost @ 2004-07-12  3:39 UTC (permalink / raw)
  To: ayerkes; +Cc: caml-list

You shouldn't have to package up a non-caml managed pointer in any
sort of caml structure at all.  Take a look at section 18.6 of the
manual, specifically the definitions of curses_initscr and
curses_wrefresh.  Any pointer that's outside the caml heap (that is,
pretty much any pointer that you're getting from a non-caml function)
can simply be treated as opaque, and you can use Caml's type system to
make sure it's valid (as long as the C code always handles these
pointers correctly.)

I'd write your code like the following, based on that:

Caml code:
type sat_manager
external zchaff_InitManager : unit -> sat_manager
external zchall_ReadCnf : sat_manager -> string -> unit

C++ code:
value zchaff_InitManager(void)
{
  CAMLparam0();
  CAMLreturn((value) SAT_InitManager());
}

void zchaff_ReadCnf(value mng, value filename)
{
  CAMLparam2(mng, filename);
  SAT_Manager solver = (void*)mng;
  cout<<"solver = "<<hex <<solver <<endl;
  assert(solver != NULL);  
  char * fn = String_val(filename);  
  cout<<"file = "<<fn <<endl;
  read_cnf(solver, fn);
  CAMLreturn0;
}

One thing I wonder about, though, is the line:

  SAT_Manager solver = (void*)mng;

shouldn't you be casting to something other than (void*) here?  Not
that I know anything about how the type SAT_Manager is represented.

John.

-------------------
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


^ permalink raw reply	[flat|nested] 14+ messages in thread
* [Caml-list] Toplevel crashes when trying to call external functions
@ 2004-07-11  1:20 Andy Yang
  2004-07-11 18:14 ` art yerkes
  2004-07-12  6:55 ` Anne Pacalet
  0 siblings, 2 replies; 14+ messages in thread
From: Andy Yang @ 2004-07-11  1:20 UTC (permalink / raw)
  To: caml-list

Hi, all

I am relatively new to Ocaml. Sorry about the spam if
this is a trivial problem. I am trying to give call
some external functions. 

Some codes are as follows:

value zchaff_InitManager(void)
{
  CAMLparam0();  
  void * solver = SAT_InitManager();
  value val = alloc(1, Custom_tag);
  Int32_val(val) = (int) solver;
  CAMLreturn ( val );
}

void zchaff_ReadCnf(value mng, value filename)
{
  CAMLparam2(mng, filename);
  void* solver = (void*)Int32_val(mng);
  char * fn = String_val(filename);
  read_cnf(solver, fn);  
  CAMLreturn0;
}

When I call these functions in a .ml file. It is okay.
.However, when I tried to call these functions
interactively, segmentation fault happens. The
interactive way is as follows:

# let solver = zchaff_InitManager () ;;
solver = 0x8092328
val solver : zchaff_solver = <abstr>
# Gc.full_major();;
- : unit = ()
# solver ;;
- : zchaff_solver = <abstr>
# let _ = zchaff_ReadCnf solver "testcase/4.cnf";;
solver = 0x400
file = testcase/4.cnf
Segmentation fault

It seems that the variable "solver" is not registered
in Gc's root tree. So solver's value changes after
Gc.full_major(). Where are wrong in my codes? Tracing
with gdb, the stack is as follows when segmentation
fault happens.

#0  0x0807120f in obj_tag ()
#1  0x08074c8f in interprete ()
#2  0x08075dbe in caml_main ()
#3  0x08067a9c in main ()

Thanks a lot!

Andy





		
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail 

-------------------
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


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

end of thread, other threads:[~2004-07-12 15:39 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-12  3:39 [Caml-list] Toplevel crashes when trying to call external functions John Prevost
2004-07-12  4:46 ` Andy Yang
2004-07-12 10:55 ` Gerd Stolpmann
2004-07-12 12:03   ` Richard Jones
2004-07-12 12:55     ` Jean-Christophe Filliatre
  -- strict thread matches above, loose matches on Subject: below --
2004-07-11  1:20 Andy Yang
2004-07-11 18:14 ` art yerkes
2004-07-11 18:58   ` Andy Yang
2004-07-12 11:04     ` Gerd Stolpmann
2004-07-12 11:35     ` Damien Doligez
2004-07-12 13:16       ` Andy Yang
2004-07-12 13:33         ` Olivier Andrieu
2004-07-12 15:39           ` Andy Yang
2004-07-12  6:55 ` Anne Pacalet

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).