Re: Help passing D strings to C libs

2011-03-14 Thread Daniel Green

On 3/14/2011 1:38 AM, Gene P. Cross wrote:

I've amended the source to pass the strings pointer (path.ptr) after adding a 
null
but the problem still persists.

I lost for what it could be and I'm certain this is where the problem is, 
because
if I remove the method call, the program runs fine.

I've noticed that calling SDL_LoadBMP and passing a string literal seems to 
work,
instead of a string variable. I might load all the images independently into an
array and have each object reference that array, instead of every object loading
its own.


It sounds like a bug with version of D your using.  I make heavy use of 
toStringz with a lua wrapper I have.  If you can debug your program, try 
doing something like this.


char* ptr = toStringz(path);
SDL_LoadBMP(ptr);

and inspect ptr before the call to SDL_LoadBMP.  I would also replace 
string with char[].  To be sure that string isn't really wchar[] or 
dchar[].  Although, I imagine the compiler would catch that.


Re: Help passing D strings to C libs

2011-03-14 Thread Jonathan M Davis
On Sunday 13 March 2011 22:38:49 Gene P. Cross wrote:
 I've amended the source to pass the strings pointer (path.ptr) after adding
 a null but the problem still persists.
 
 I lost for what it could be and I'm certain this is where the problem is,
 because if I remove the method call, the program runs fine.
 
 I've noticed that calling SDL_LoadBMP and passing a string literal seems to
 work, instead of a string variable. I might load all the images
 independently into an array and have each object reference that array,
 instead of every object loading its own.

In D2 (and I assume D1, but I don't know), string literals all have \0 one 
past their end, so you can safely pass them to C functions. Other strings don't 
have them, so you have to append the \0. Other than that, I don't know why 
there would be any difference between passing a string literal and a regular 
string. Perhaps something else in your code is altering the string later (which 
it wouldn't do if you just passed a literal) and _that_ is screwing it up. I 
don't know. For the most part, there shouldn't be much difference between 
dealing 
with string literals and dealing with normal strings.

- Jonathan M Davis


Re: Help passing D strings to C libs

2011-03-14 Thread Gene P. Cross
-Daniel
I tried what you said:

char* ptr = toStringz(path);
SDL_LoadBMP(ptr);

and made a check to see if the pointer is null, which it isn't, but I'm unable 
to
inspect is value, I haven't a debugger at the moment, could you recommend one ?

I also made the string a char[] and tested to see if that made a difference.

I think it may be something to do with SDL itself. I tried calling another
function, SDL_GetTicks(), and that's causing problems as well.


-Jonathan
I read the docs earlier and found the same thing about literals being null
terminated but not regular strings,
which explains why they work.
Double check after double check, I am also certain that no other code is messing
with it and changing values.


Re: Help passing D strings to C libs

2011-03-14 Thread Gene P. Cross
I found the problem.

I've set up my 'main' file to act on various game states and because my load 
state
is physically below the running state (where the problems were occuring), even
though they were getting called first, the program starts in the loading state,
dmd wasn't having it. I tried moving the load state above the rest, and it works
fine. So sorry to have wasted everybody's time with such a simple and stupid
mistake. Thankyou for all your help.


Re: Help passing D strings to C libs

2011-03-14 Thread Daniel Green

On 3/14/2011 2:55 AM, Gene P. Cross wrote:

I haven't a debugger at the moment, could you recommend one ?
Sorry, I use GDB with GDC.  I don't know about DMD, it may work with GDB 
on linux.  You could try installing GDC and see if the results are the 
same.  That may help to rule out the compiler itself.



I also made the string a char[] and tested to see if that made a difference.

That also requires using some of the previous examples to append '\0' first.


I think it may be something to do with SDL itself. I tried calling another
function, SDL_GetTicks(), and that's causing problems as well.
Can you install a program that uses SDL and are you running 64-bit? 
Could be possible you're trying to call a 64-bit function from 32-bit code.



I read the docs earlier and found the same thing about literals being null
terminated but not regular strings,

Every suggestion so far should take care of that issue.



Re: Help passing D strings to C libs

2011-03-14 Thread Daniel Green

On 3/14/2011 3:07 AM, Gene P. Cross wrote:

I found the problem.

I've set up my 'main' file to act on various game states and because my load 
state
is physically below the running state (where the problems were occuring), even
though they were getting called first, the program starts in the loading state,
dmd wasn't having it. I tried moving the load state above the rest, and it works
fine. So sorry to have wasted everybody's time with such a simple and stupid
mistake. Thankyou for all your help.
That sounds like an issue, I've ran into on occasion.  When using 
runtime bindings for shared libraries.  If you forget to initialize them 
in every module first thing.  The program will segfault.


Re: Help passing D strings to C libs

2011-03-14 Thread Jonathan M Davis
On Monday 14 March 2011 00:07:05 Gene P. Cross wrote:
 I found the problem.
 
 I've set up my 'main' file to act on various game states and because my
 load state is physically below the running state (where the problems were
 occuring), even though they were getting called first, the program starts
 in the loading state, dmd wasn't having it. I tried moving the load state
 above the rest, and it works fine. So sorry to have wasted everybody's
 time with such a simple and stupid mistake. Thankyou for all your help.

It happens to us all from time to time. At least you found the problem.

- Jonathan M Davis


Re: Help passing D strings to C libs

2011-03-14 Thread Gene P. Cross
Thanks for your help, and from here on in I'll be sure to initialise first 
thing.
I'll look into GDB, thanks again.


Re: Help passing D strings to C libs

2011-03-14 Thread spir

On 03/14/2011 07:55 AM, Gene P. Cross wrote:

-Daniel
I tried what you said:

char* ptr = toStringz(path);
SDL_LoadBMP(ptr);

and made a check to see if the pointer is null, which it isn't, but I'm unable 
to
inspect is value, I haven't a debugger at the moment, could you recommend one ?

I also made the string a char[] and tested to see if that made a difference.

I think it may be something to do with SDL itself. I tried calling another
function, SDL_GetTicks(), and that's causing problems as well.


-Jonathan
I read the docs earlier and found the same thing about literals being null
terminated but not regular strings,
which explains why they work.
Double check after double check, I am also certain that no other code is messing
with it and changing values.


I'm surprised about your problem. There is no difference between the .ptr 
property of a null-terminated D char[] and a C string (hum). And I'm rather 
sure (not checked the code) that's precisely what toStringZ does. The issue 
must lie on some point you have not mentionned yet.


Denis
--
_
vita es estrany
spir.wikidot.com



Re: Help passing D strings to C libs

2011-03-14 Thread Trass3r

I'm having trouble passing D strings (char[]) to SDL, in particular
SDL_LoadBMP(), I keep receiving a segfault.

Heres the code:

void setImg(string path) {
// concat null terminating char to string and cast to c type string  
when

// passing to SDL_LoadBMP()
path ~= \0;
image = SDL_LoadBMP( cast(char*)path );
}

and the value of path is ./resources/cannon.bmp

I'm using SDL 1.2.14 and DMD 1.067 on Ubuntu 10.10


Well strings are put into read-only space on Linux and I guess this is  
also the case for D1.

Since you are doing a ~= it probably tries to alter that value and crashes.

You should use something like SDL_LoadBMP(cast(char*)(path ~ \0)) if you  
really wanted to convert it manually.

But the proper way to convert a string to a C char* is to use toStringz.


Re: Help passing D strings to C libs

2011-03-14 Thread Jonathan M Davis
On Monday, March 14, 2011 10:54:57 Trass3r wrote:
  I'm having trouble passing D strings (char[]) to SDL, in particular
  SDL_LoadBMP(), I keep receiving a segfault.
  
  Heres the code:
  
  void setImg(string path) {
  
  // concat null terminating char to string and cast to c type string
  
  when
  
  // passing to SDL_LoadBMP()
  path ~= \0;
  image = SDL_LoadBMP( cast(char*)path );
  
  }
  
  and the value of path is ./resources/cannon.bmp
  
  I'm using SDL 1.2.14 and DMD 1.067 on Ubuntu 10.10
 
 Well strings are put into read-only space on Linux and I guess this is
 also the case for D1.
 Since you are doing a ~= it probably tries to alter that value and crashes.

No. If it can't concatenate in place, then it will reallocate the array. The 
fact that it was in read-only memory is irrelevant. It just means that 
reallocation is guaranteed instead of being only possible.

- Jonathan M Davis


Re: Help passing D strings to C libs

2011-03-13 Thread Andrej Mitrovic
Use toStringz from std.string, ala:
SDL_LoadBMP(toStringz(path));

Do not embed nulls before calling toStringz, so remove 'path ~= \0;'


Re: Help passing D strings to C libs

2011-03-13 Thread Andrej Mitrovic
Wait, actually I'm not sure if there's toStringz for D1, it is there
for D2. Try it out, I guess.


Re: Help passing D strings to C libs

2011-03-13 Thread Gene P. Cross
toStringz is in D1 but still no luck, I still get the same error

Thanks for the suggestion though


Re: Help passing D strings to C libs

2011-03-13 Thread Jonathan M Davis
On Sunday 13 March 2011 21:32:49 Gene P. Cross wrote:
 Hi, I'm fairly new to D and I'm writing a Space Invaders clone to get
 myself acquainted with the language.
 
 I'm having trouble passing D strings (char[]) to SDL, in particular
 SDL_LoadBMP(), I keep receiving a segfault.
 
 Heres the code:
 
 void setImg(string path) {
 // concat null terminating char to string and cast to c type string
 when // passing to SDL_LoadBMP()
 path ~= \0;
 image = SDL_LoadBMP( cast(char*)path );
 }
 
 and the value of path is ./resources/cannon.bmp
 
 I'm using SDL 1.2.14 and DMD 1.067 on Ubuntu 10.10
 
 If someone could tell me what I'm doing wrong it would be greatly
 appreciated.

I don't use D1, so I don't know all of the ins and outs, but you're going to 
have to make sure that you put a \0 at the end of the string so that it's 
properly null-terminated (which you appear to be doing), and you should be 
passing the string's ptr property to the function, not casting the string to 
char*.

If you were dealing with D2, you'd also have to worry about not passing a 
string 
(as opposed to a char[]) to a C function if there's _any_ chance that it would 
alter it, since string in D2 is immutable(char)[], but that's not the case in 
D1, so that shouldn't be a problem with what you're doing.

- Jonathan M Davis


Re: Help passing D strings to C libs

2011-03-13 Thread Gene P. Cross
I've amended the source to pass the strings pointer (path.ptr) after adding a 
null
but the problem still persists.

I lost for what it could be and I'm certain this is where the problem is, 
because
if I remove the method call, the program runs fine.

I've noticed that calling SDL_LoadBMP and passing a string literal seems to 
work,
instead of a string variable. I might load all the images independently into an
array and have each object reference that array, instead of every object loading
its own.