Re: FFI callback is broken in last git version
Kostirya <[email protected]> Fri, 30 Sep 2016 11:12:33 +0300
| Newsgroups | gmane.comp.lang.ml.mlton.user |
|---|---|
| Message-ID | <CAG+QnTeKd_GwMHDF3eSNcC36CMf4-GqM+L4PAmyFq1Rpm730tg@mail.gmail.com> |
Whew, finally we have sorted it out. Now I understand why the version of this program for PolyML worked in this situation. Transitional memory is used in PolyML, not the SML pointer is transmitted to C. By the way, I have been searching recently information about this in MLton and I failed to find it. Now I will know that the direct SML pointer transferred to C. Thanks you very much! 2016-09-29 20:16 GMT+03:00 Matthew Fluet <[email protected]>: > Are you seeing the segfault with exactly the code you posted earlier, > or only in your larger example? Since you've switched to using > malloc, I wonder if in your larger example, you are holding onto the > pointer passed to the C code in a C-side data structure; that will not > work. We cannot give to C a pointer to an ML object, return to SML, > then call back to a C function and expect the pointer to still be > valid --- an ML garbage collection may have occurred during the SML > code which will have moved the ML object and quite possibly > deallocated the heap in which the original pointer was pointing. If > you want to retain a data structure on the C side that is also > available on the ML side, then you need to something like allocate it > from the C heap with malloc (like you've done in the revised example). > > On Thu, Sep 29, 2016 at 11:17 AM, Kostirya <[email protected]> wrote: > > I replaced it with the manual malloc, so it started working excellently. > > > > It is like: > > > > val foo_ffi = _import "foo" reentrant: t -> unit; > > > > val malloc = (_import "malloc": Word.word -> t;) o Word.fromInt > > val free = _import "free": t -> unit; > > > > fun foo () = > > let > > val r_mem = malloc(4) > > val _ = setInt32(r_mem, 0, 0) > > val _ = foo_ffi(r_mem) > > val r = getInt32(r_mem, 0) > > val _ = free r_mem > > in > > r > > end > > > > > > > > 2016-09-29 17:19 GMT+03:00 Kostirya <[email protected]>: > >> > >> 2016-09-29 16:56 GMT+03:00 Matthew Fluet <[email protected]>: > >> > > >> > In your example, you _import "foo", but your C code defines "baz". > >> > >> It was a typo in the code example. > >> > >> > > >> > But, other than that, I don't see anything wrong with the code, and > >> > you should be able to pass an "int ref" to a C function, update it, > >> > and then observe the update back in the SML code. > >> > >> That's right, it works in most calls, however it fails sometimes. The > >> error only occurs on 64bit OS. > >> > >> > >> > > >> > > >> > On Thu, Sep 29, 2016 at 9:41 AM, Kostirya <[email protected]> wrote: > >> > > I found, thank you. > >> > > But I do not know how to fix it. > >> > > I'll try to describe on simplified example > >> > > > >> > > SML code: > >> > > > >> > > val foo_ffi = _import "foo" reentrant: int ref -> unit; > >> > > > >> > > fun foo () = > >> > > let > >> > > val r = ref 0 > >> > > val _ = foo_ffi(r) > >> > > in > >> > > !r > >> > > end > >> > > > >> > > C code: > >> > > > >> > > void baz (int * r) { > >> > > *r = 10; // There is Segmentation fault !!! > >> > > return; > >> > > } > >> > > > >> > > gdb: > >> > > #4 0x00000000004212b4 in MLton_init (argc=<error reading variable: > >> > > Cannot > >> > > access memory at address 0x7ff2ce3ef174>, > >> > > argv=<error reading variable: Cannot access memory at address > >> > > 0x7ff2ce3ef168>, > >> > > s=<error reading variable: Cannot access memory at address > >> > > 0x7ff2ce3ef160>) at platform.c:20 > >> > > Backtrace stopped: previous frame inner to this frame (corrupt > stack?) > >> > > > >> > > N.B. This only for 64 bit Linux. 32 bit - OK. > >> > > > > -- > > You received this message because you are subscribed to the Google Groups > > "MLton-user" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to [email protected]. > > > > ------------------------------------------------------------ > ------------------ > > > > _______________________________________________ > > MLton-user mailing list > > [email protected]; [email protected] > > https://lists.sourceforge.net/lists/listinfo/mlton-user > > > > -- > You received this message because you are subscribed to the Google Groups > "MLton-user" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > > > ------------------------------------------------------------ > ------------------ > _______________________________________________ > MLton-user mailing list > [email protected]; [email protected] > https://lists.sourceforge.net/lists/listinfo/mlton-user > -- You received this message because you are subscribed to the Google Groups "MLton-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. ------------------------------------------------------------------------------ _______________________________________________ MLton-user mailing list [email protected]; [email protected] https://lists.sourceforge.net/lists/listinfo/mlton-user