www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem with interfacing C code to D

reply bioinfornatics <bioinfornatics fedoraproject.org> writes:
Dear i do not understand why the first example works and the second
segfault. Thanks

----------------EXAMPLE 1 -------------------
// ldc2 -L=3D/usr/lib64/libXlib.so -L-lX11 -g -w xtest.d=20
import std.string;
import std.stdio;
import std.conv;
import std.c.stdlib : getenv;
import std.exception : Exception;

import X11.Xlib;

void main( string[] args ){
    Display* display =3D XOpenDisplay(getenv("DISPLAY"));
    if( display is null )
        new Exception( "Could not communicate with X server" );
    int counter;
    string pattern =3D "*\0";
    char** fonts   =3D XListFonts( display, pattern.dup.ptr, 10, &counter
);
    for(int c =3D 0; c < counter; c++)
        writeln( to!string( fonts[c] ) );
}


----------------EXAMPLE 2 -------------------
// ldc2 -L=3D/usr/lib64/libXlib.so -L-lX11 -g -w xtest.d=20
import std.string;
import std.stdio;
import std.conv;
import std.c.stdlib : getenv;
import std.exception : Exception;

import X11.Xlib;

void main( string[] args ){
    Display display =3D *XOpenDisplay(getenv("DISPLAY"));
    if( display is null )
        new Exception( "Could not communicate with X server" );
    int counter;
    string pattern =3D "*\0";
    char** fonts   =3D XListFonts( &display, pattern.dup.ptr, 10, &counter
);
    for(int c =3D 0; c < counter; c++)
        writeln( to!string( fonts[c] ) );
}
Jan 09 2012
next sibling parent reply Trass3r <un known.com> writes:
What's the definition of Display?
Jan 09 2012
next sibling parent bioinfornatics <bioinfornatics fedoraproject.org> writes:
Le mardi 10 janvier 2012 =C3=A0 01:26 +0100, Trass3r a =C3=A9crit :
 What's the definition of Display?

This one: _____________________________ struct _XDisplay{ XExtData* ext_data; /* hook for extension to hang data */ _XPrivate* private1; int fd; /* Network socket. */ int private2; int proto_major_version; /* major version of server's X protocol */ int proto_minor_version; /* minor version of servers X protocol */ char* vendor; /* vendor of the server hardware */ XID private3; XID private4; XID private5; int private6; XID function(_XDisplay*)resource_alloc; /* allocator function */ int char_order; /* screen char order, LSBFirst, MSBFirst */ int bitmap_unit; /* padding and data requirements */ int bitmap_pad; /* padding requirements on bitmaps */ int bitmap_bit_order; /* LeastSignificant or MostSignificant */ int nformats; /* number of pixmap formats in list */ ScreenFormat* pixmap_format; /* pixmap format list */ int private8; int release; /* release of the server */ _XPrivate* private9, private10; int qlen; /* Length of input event queue */ c_ulong last_request_read; /* seq number of last event read */ c_ulong request; /* sequence number of last request. */ XPointer private11; XPointer private12; XPointer private13; XPointer private14; uint max_request_size; /* maximum number 32 bit words in request*/ _XrmHashBucketRec* db; int function( _XDisplay* )private15; char* display_name; /* "host:display" string used on this connect*/ int default_screen; /* default screen for operations */ int nscreens; /* number of screens on this server*/ Screen* screens; /* pointer to list of screens */ c_ulong motion_buffer; /* size of motion buffer */ c_ulong private16; int min_keycode; /* minimum defined keycode */ int max_keycode; /* maximum defined keycode */ XPointer private17; XPointer private18; int private19; char* xdefaults; /* contents of defaults from server */ /* there is more to this structure, but it is private to Xlib */ } alias _XDisplay Display;
Jan 09 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
It's likely a module Gmail - Problem with interfacing C code to D -
Mozilla Firefo;

import std.algorithm;
import std.array;
import std.stdio;

void main()
{

}



On 1/10/12, bioinfornatics <bioinfornatics fedoraproject.org> wrote:
 Le mardi 10 janvier 2012 =E0 01:26 +0100, Trass3r a =E9crit :
 What's the definition of Display?

This one: _____________________________ struct _XDisplay{ XExtData* ext_data; /* hook for extension to hang data */ _XPrivate* private1; int fd; /* Network socket. */ int private2; int proto_major_version; /* major version of server's X protocol */ int proto_minor_version; /* minor version of servers X protocol */ char* vendor; /* vendor of the server hardware */ XID private3; XID private4; XID private5; int private6; XID function(_XDisplay*)resource_alloc; /* allocator function */ int char_order; /* screen char order, LSBFirst, MSBFirst */ int bitmap_unit; /* padding and data requirements */ int bitmap_pad; /* padding requirements on bitmaps */ int bitmap_bit_order; /* LeastSignificant or MostSignificant */ int nformats; /* number of pixmap formats in list */ ScreenFormat* pixmap_format; /* pixmap format list */ int private8; int release; /* release of the server */ _XPrivate* private9, private10; int qlen; /* Length of input event queue */ c_ulong last_request_read; /* seq number of last event read */ c_ulong request; /* sequence number of last request. */ XPointer private11; XPointer private12; XPointer private13; XPointer private14; uint max_request_size; /* maximum number 32 bit words in request*/ _XrmHashBucketRec* db; int function( _XDisplay* )private15; char* display_name; /* "host:display" string used on this connect*/ int default_screen; /* default screen for operations */ int nscreens; /* number of screens on this server*/ Screen* screens; /* pointer to list of screens */ c_ulong motion_buffer; /* size of motion buffer */ c_ulong private16; int min_keycode; /* minimum defined keycode */ int max_keycode; /* maximum defined keycode */ XPointer private17; XPointer private18; int private19; char* xdefaults; /* contents of defaults from server */ /* there is more to this structure, but it is private to Xlib */ } alias _XDisplay Display;

Jan 09 2012
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Jesus christ, sorry about that my keyboard script went crazy and posted that.

What I was going to say is it's likely a mismatch of the struct sizes.
In the second example you are dereferencing the pointer on the D size,
which does a field-by-field copy of the pointed-to struct. But D will
only read the exact amount of bytes that is defined by the structure.
So if you end up reading e.g 176 bytes, but the C structure is
actually more than that, then the C-side (X11) will end up reading
into D memory past the 176th byte, and will segfault.

If you can, try seeing if you can get the size of the Display
structure in C code (e.g. use printf("%d", sizeof(Display))), and then
compare that to D with writeln(Display.sizeof). They must have the
same size.
Jan 09 2012
prev sibling next sibling parent Artur Skawina <art.08.09 gmail.com> writes:
On 01/10/12 01:02, bioinfornatics wrote:
 Dear i do not understand why the first example works and the second
 segfault. Thanks
 
 ----------------EXAMPLE 1 -------------------

     char** fonts   = XListFonts( display, pattern.dup.ptr, 10, &counter

 ----------------EXAMPLE 2 -------------------
     Display display = *XOpenDisplay(getenv("DISPLAY"));

     char** fonts   = XListFonts( &display, pattern.dup.ptr, 10, &counter

Do you expect the xlib functions to work with copies of private structures? artur
Jan 09 2012
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Scratch that, in X11 apparently the Display structure is an incomplete
type (so sizeof won't work). This means you most probably *have* to
use pass it around as an opaque pointer. It's kind of odd because you
can still access some of its fields (so it's not totally opaque), but
you can't do copies.

So just use pointers and you'll be safe.

Hell I've never really done any C programming except what's in K&R,
but this seems like a logical conclusion to me..
Jan 09 2012