www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C -> D conversion segfault

reply teqDruid <me teqdruid.com> writes:
I'm attempting to write some D bindings, however I'm having trouble
porting some code.  The output (correct output) from the C version is
this:
showEnv:   No Fault
showEnv:   XMLRPC-Fault: test (3)

Where as the output from the D Version is:
showEnv:   No Fault
Segmentation fault

In various other places, things like this have been happening, where this
xmlrpc_env struct becomes corrupted, and causes these segfaults.  I'm not
sure what I'm doing wrong... or perhaps it's not my bug?

I'm running DMD 0.97 on Linux

TIA
John

Here's the code:

 -----C Version---------
typedef signed int xmlrpc_int32;

typedef struct _xmlrpc_env {
    int   fault_occurred;
    xmlrpc_int32 fault_code;
    char* fault_string;
} xmlrpc_env;

extern void xmlrpc_env_init (xmlrpc_env* env);
extern void xmlrpc_env_set_fault (xmlrpc_env *env, int code, char *string);

void showEnv(xmlrpc_env* env)
{
	printf("showEnv:   ");
	if (env->fault_occurred)
		printf("XMLRPC-Fault: %s (%d)\n", env->fault_string, env->fault_code);
	else
		printf("No Fault\n");
}

int main(void)
{
	xmlrpc_env env;
	xmlrpc_env_init(&env);
	showEnv(&env);
	xmlrpc_env_set_fault(&env, 3, "test");
	showEnv(&env);
	
	return 0;
}
--------End C Version-------------

--------D Version---------------
typedef int xmlrpc_int32;

struct xmlrpc_env {
    int   fault_occurred;
    xmlrpc_int32 fault_code;
    char* fault_string;
}

extern(C) void xmlrpc_env_init (xmlrpc_env* env);
extern(C) void xmlrpc_env_set_fault (xmlrpc_env *env, int code, char *string);

void showEnv(xmlrpc_env* env)
{
	printf("showEnv:   \0");
	if (env.fault_occurred)
		printf("XMLRPC-Fault: %.*s (%d)\n\0", env.fault_string, env.fault_code);
	else
		printf("No Fault\n\0");
}

void main()
{
	xmlrpc_env env;
	xmlrpc_env_init(&env);
	showEnv(&env);
	xmlrpc_env_set_fault(&env, 3, "test\0");
	showEnv(&env);
}
------------End D Version
Aug 03 2004
next sibling parent reply Arcane Jill <Arcane_member pathlink.com> writes:
In article <pan.2004.08.03.21.10.08.997592 teqdruid.com>, teqDruid says...

Just a thought, but maybe:



should be



because env.fault_string is a (char*), not a (char[]).

(Fingers crossed)
Jill
Aug 03 2004
parent teqDruid <me teqdruid.com> writes:
Yeah... that did it.  Actually, I had hoped that this was a good test case
for the other problems I've been having... but I guess not.  I don't think
this'll be the end of this, but thanks... for now.

John

On Tue, 03 Aug 2004 21:22:10 +0000, Arcane Jill wrote:

 In article <pan.2004.08.03.21.10.08.997592 teqdruid.com>, teqDruid says...
 
 Just a thought, but maybe:
 

 
 should be
 

 
 because env.fault_string is a (char*), not a (char[]).
 
 (Fingers crossed)
 Jill
Aug 03 2004
prev sibling parent teqDruid <me teqdruid.com> writes:
I was hoping that the previous post was a shorted test case of something
like this.  Here, print_state_name_callback is a callback that the C
library calls.  The pointer to an xmlrpc_env struct is... not right.  I'm
not sure what's wrong with it, but the output (from the
die_if_fault_occurred function) is:
env.fault_occurred: 1886680168
Segmentation fault

I can post xmlrpcd.xmlrpcd and xmlrpcd.client if necessary, but they're
rather long.  They're ports of xmlrpc.h and xmlrpc-client.h.  The D
program below is a slightly modified port of:
http://xmlrpc-c.sourceforge.net/examples/asynch_client.c

The C version works fine.

Any advice?

TIA
John



import xmlrpcd.xmlrpcd;
import xmlrpcd.client;

void die_if_fault_occurred (xmlrpc_env* env)
{
	printf("env.fault_occurred: %d\n\0", env.fault_occurred);
   	if (env.fault_occurred) {
	    printf("XML-RPC Fault:\0");
        printf("%s (%d)\n\0",
                env.fault_string, env.fault_code);
        throw new Exception("XML-RPC Fault");
    }
}

static void print_state_name_callback (char *server_url,
				       char *method_name,
				       xmlrpc_value *param_array,
				       void *user_data,
				       xmlrpc_env* env,
				       xmlrpc_value* result)
{
    int state_number;
    char *state_name;

    /* Check to see if a fault occurred. */
    die_if_fault_occurred(env);
	
    /* Get our state name. */
    xmlrpc_parse_value(env, result, "s", &state_name);	    
    die_if_fault_occurred(env);
    
    printf("State: %s\n", state_name);
}
	
void main()
{
	
    xmlrpc_value *result;
    char *state_name;
    int i;
    
    /* Start up our XML-RPC client library. */
    xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, "Name", "Version");
    
    /* Make a whole bunch of asynch calls. */
    for (i = 40; i < 45; i++) {
	xmlrpc_client_call_asynch("http://localhost:8080/RPC2",
				  "mirror.Random",
				  &print_state_name_callback, null,
				  "()");
    }

    /* Wait for all calls to complete. */
    xmlrpc_client_event_loop_finish_asynch();

    /* shutdown our XML-RPC client library. */
    xmlrpc_client_cleanup();	
}

On Tue, 03 Aug 2004 17:10:09 -0400, teqDruid wrote:

 In various other places, things like this have been happening, where this
 xmlrpc_env struct becomes corrupted, and causes these segfaults.  I'm not
 sure what I'm doing wrong... or perhaps it's not my bug?
 
 I'm running DMD 0.97 on Linux
Aug 03 2004