Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Crashes when D string passed to C function using .ptr #44

Closed
growlercab opened this issue May 11, 2015 · 5 comments
Closed

Crashes when D string passed to C function using .ptr #44

growlercab opened this issue May 11, 2015 · 5 comments

Comments

@growlercab
Copy link
Contributor

There a number of places in Dgame where D strings are passed to D functions using some_string.ptr. I think the .ptr needs to be replaced with .toStringz. I'm not sure because the crashes are not on all calls to C functions taking a D string via .ptr so it could be a bug in my own code.

But I think it's work looking into because D strings are not 0-terminated and .ptr, as I understand it, doesn't append '\0'. That's what toStringz is for.

I see errors like the following...

E.g. in my code I have

auto s = Surface("my_image.png");

This results in a call to Surface.d::loadFrmoFile(string filename), which in turn calls _surface = IMG_Load(filename.ptr) with the following runtime result...

Could not load image my_img.png�PowerInfo.

The '�PowerInfo' part is where IMG_Load(filename.ptr); has run off the end of the string because it's missing the null terminator.

My workaround for now is to manually add an ugly \0 to the D strings like so:

auto s = Surface("my_image.png\0");

@Dgame
Copy link
Owner

Dgame commented May 11, 2015

Hey,
this is fixed in ~master by commit a6d32e7

It will be included with the upcomming Dgame 0.6.0 if I finally found the time to test the last piece (VertexArray) and I finish the new website (http://rswhite.de/dgame6/).

But I leave that open, maybe you find places which I've overlooked.

@Dgame
Copy link
Owner

Dgame commented May 11, 2015

BTW I'm not using D's toStringz because it is a waste of GC memory and therefore not @nogc.

Should I maybe release a preview release?

@growlercab
Copy link
Contributor Author

Great work and the new website looks great too :)

I'll switch over to master and give it a test and if all goes well I'll close this off.

If it's quick and easy to create a release then a 0.6.0-RC might help to recruit some testers for a short sprint, but I don't think it's essential.

@growlercab
Copy link
Contributor Author

Building all my projects with Dgame ~master I no longer experience any crashes and all the strings look good when I view the memory in GDB.

Thanks for the help!

@Dgame
Copy link
Owner

Dgame commented May 12, 2015

My pleasure. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants