www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Random behavior using a wrapped C library

reply simendsjo <simendsjo gmail.com> writes:
I'm getting some unexpected results (crashes or random behavior) when  
using a wrapped C struct in a library. I have no idea why this is  
happening as everything else (including other wrapped structs) seems to  
work just fine.
Could it be some alignment issues..?

extern(C):

//typedef unsigned char uint8;
alias ubyte uint8;

//typedef struct {
//	uint8 r,g,b;
//} TCOD_color_t;
struct TCOD_color_t {
	uint8 r,g,b;
};

//typedef void * TCOD_console_t;
alias void* TCOD_console_t;

//TCODLIB_API void TCOD_console_set_default_background(TCOD_console_t  
con,TCOD_color_t col);
void TCOD_console_set_default_background(TCOD_console_t con,TCOD_color_t  
col);

Whenever I use I call this function using TCOD_color_t(255,255,255) it  
seems to set a random other value.
Also, when calling get_default_background (not shown here), I get random  
results back.
I know the library works fine from C, so it has to be somewhere in my  
wrapper.

Does anyone spot any obvious flaws in my wrapping above? Or has any other  
hints on what might have gone wrong?

I've only tested this on linux64.

Thanks.
Feb 28 2012
parent reply Trass3r <un known.com> writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5570
Feb 28 2012
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On Tue, 28 Feb 2012 16:58:13 +0100, Trass3r <un known.com> wrote:

 http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thanks. I've literally spent hours testing various things without any luck - would have been simpler if I knew asm :/ A blocker for using x64 on linux then.
Feb 28 2012
parent Mike Parker <aldacron gmail.com> writes:
On 2/29/2012 1:10 AM, simendsjo wrote:
 On Tue, 28 Feb 2012 16:58:13 +0100, Trass3r <un known.com> wrote:

 http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thanks. I've literally spent hours testing various things without any luck - would have been simpler if I knew asm :/ A blocker for using x64 on linux then.

Have you tested on 32-bit yet? I had the exact same problem with one of my wrappers when passing a struct by value, but on 32-bit Windows. Luckily, in my case it was simple enough to implement the function in D to work around it. I'm curious to know if it's related.
Feb 28 2012
prev sibling next sibling parent Trass3r <un known.com> writes:
 A blocker for using x64 on linux then.

Use gdc. Better codegen anyway.
Feb 28 2012
prev sibling next sibling parent simendsjo <simendsjo gmail.com> writes:
On Wed, 29 Feb 2012 05:03:30 +0100, Mike Parker <aldacron gmail.com> wrote:

 On 2/29/2012 1:10 AM, simendsjo wrote:
 On Tue, 28 Feb 2012 16:58:13 +0100, Trass3r <un known.com> wrote:

 http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thanks. I've literally spent hours testing various things without any luck - would have been simpler if I knew asm :/ A blocker for using x64 on linux then.

Have you tested on 32-bit yet? I had the exact same problem with one of my wrappers when passing a struct by value, but on 32-bit Windows. Luckily, in my case it was simple enough to implement the function in D to work around it. I'm curious to know if it's related.

Yes. Tested on x32, and it works just fine. I'll use ia32libs and -m32 for the time being then. A strange thing is that memory consumption went _up_ when everything was compiled as x32.
Feb 28 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
On 29 February 2012 19:30, simendsjo <simendsjo gmail.com> wrote:
 On Wed, 29 Feb 2012 05:03:30 +0100, Mike Parker <aldacron gmail.com> wrote:

 On 2/29/2012 1:10 AM, simendsjo wrote:
 On Tue, 28 Feb 2012 16:58:13 +0100, Trass3r <un known.com> wrote:

 http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thanks. I've literally spent hours testing various things without any luck - would have been simpler if I knew asm :/ A blocker for using x64 on linux then.

Have you tested on 32-bit yet? I had the exact same problem with one of my wrappers when passing a struct by value, but on 32-bit Windows. Luckily, in my case it was simple enough to implement the function in D to work around it. I'm curious to know if it's related.

Yes. Tested on x32, and it works just fine. I'll use ia32libs and -m32 for the time being then. A strange thing is that memory consumption went _up_ when everything was compiled as x32.

I don't know much, but wouldn't bigger register sizes mean that less data needs shuffled in and out of memory? Resulting in less instructions and therefore less memory usage? I'm just guessing though -- James Miller
Feb 28 2012
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Wednesday, 29 February 2012 at 06:30:27 UTC, simendsjo wrote:
 A strange thing is that memory consumption went _up_ when 
 everything was compiled as x32.

Data/code/symbols size?
Feb 28 2012
prev sibling parent simendsjo <simendsjo gmail.com> writes:
On Wed, 29 Feb 2012 07:44:23 +0100, James Miller <james aatch.net> wrote:

 On 29 February 2012 19:30, simendsjo <simendsjo gmail.com> wrote:
 On Wed, 29 Feb 2012 05:03:30 +0100, Mike Parker <aldacron gmail.com>  
 wrote:

 On 2/29/2012 1:10 AM, simendsjo wrote:
 On Tue, 28 Feb 2012 16:58:13 +0100, Trass3r <un known.com> wrote:

 http://d.puremagic.com/issues/show_bug.cgi?id=5570

Thanks. I've literally spent hours testing various things without any luck - would have been simpler if I knew asm :/ A blocker for using x64 on linux then.

Have you tested on 32-bit yet? I had the exact same problem with one of my wrappers when passing a struct by value, but on 32-bit Windows. Luckily, in my case it was simple enough to implement the function in D to work around it. I'm curious to know if it's related.

Yes. Tested on x32, and it works just fine. I'll use ia32libs and -m32 for the time being then. A strange thing is that memory consumption went _up_ when everything was compiled as x32.

I don't know much, but wouldn't bigger register sizes mean that less data needs shuffled in and out of memory? Resulting in less instructions and therefore less memory usage? I'm just guessing though -- James Miller

Probably. Almost everything is done using structs right now. Guess it will be quite different when I get a lot of classes bouncing around.
Feb 29 2012