• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
codingJoe

Library function also used internally: segmentation fault on Linux

7 posts in this topic

Hello,

 

I am using Qt 5.1.0, but I guess this is not directly related to the use of Qt.

 

I have a library which in fact is my application. It exports many API functions. Some of those API functions are also called internally (from the library itself). That worked fine on Windows, Mac and Linux. But changing the compiler version on Linux, has caused problems. I have following API functions (example):

extern "C" __attribute__((visibility("default"))) int returnMeAZero()
{
    return(0);
}

extern "C" __attribute__((visibility("default"))) int returnMeAOne()
{
    return(1);
}

I have a client application that loads the library and dynamically binds above 2 functions with dlopen and dlsym. That client application (an executable), when started, calls "returnMeAZero" which works fine. Internally, the library sometimes calls "returnMeAOne" which works also fine. But when the library internally calls "returnMeAZero" it crashes with a segmentation fault.

 

Above example is simplified. There are several hundreds of functions similar to those above. And only those that are references in the client source code cannot be called internally by the library. That seems very strange to me. Is there any logical explanation for that behaviour?

 

0

Share this post


Link to post
Share on other sites

Does it only happen when calling a function that returns zero? It doesn't happen when you call a function that returns non-zero?

 

Also, can you post a minimal, complete example of both the application and library that will reproduce this issue?

1

Share this post


Link to post
Share on other sites

Thanks for the quick reply!

Those functions were just examples. But basically when it crashes, it does it before entering the functions (checked with prints). Here an example a little bit more elaborate:

 

Library code, file 1:

extern "C" __attribute__((visibility("default"))) int returnMeAZero()
{
    return(0);
}
extern "C" __attribute__((visibility("default"))) int returnMeAZero2()
{
    return(0);
}
extern "C" __attribute__((visibility("default"))) int returnMeAOne()
{
    return(1);
}

Library code, file 2:

void randomFunction()
{
    returnMeAOne(); // works and doesn't crash
    returnMeAZero2(); // works and doesn't crash
    returnMeAZero(); // crashes before even entering the 'returnMeAZero' function
}

Client application:

void main()
{
    // load library with dlopen
    // bind all 3 API functions in exactly the same way (with dlsym)
    returnMeAZero(); // works without crash
}

If I remove the 'returnMeAZero' function call in the client application, then the 'randomFunction' of the library never crashes and does exactly what it is supposed to. The whole thing behaves as if just having a function call to that function in the client application changes the physical location of the function in the library!

0

Share this post


Link to post
Share on other sites

When you're checking with prints, are you sure to flush the stream? Does it do this only with release builds or with debug builds? Is your program 32-bit or 64-bit? Is your library 32-bit or 64-bit? Is this x86?

 

The reason I wanted a complete example was so I could copy 'n' paste the code and run it with my debugger and carefully watch what's happening. If it's not a 100% complete (minimal) example I can't be sure you and I aren't running different code. If you can create a complete, runnable mini sample that consistently crashes for you, I'll test it on my machines.

 

(also, be sure to use int main() instead of void main(). void main() is invalid C (and C++) and should only ever be used on a freestanding implementation)

1

Share this post


Link to post
Share on other sites

Thanks again for your reply.

Well, the project is pretty big, and cutting out everything would take time, but I will consider this.

I tested a last thing in the mean time. With above mentionned piece of example code, I tried this:

 

In the library:

printf("giveMeAZero (internal): %i\n",giveMeAZero);
printf("giveMeAZero2 (internal): %i\n",giveMeAZero2);
printf("giveMeAOne (internal): %i\n",giveMeAOne);

In the client application:

printf("giveMeAZero (clientApp): %i\n",giveMeAZero);
printf("giveMeAZero2 (clientApp): %i\n",giveMeAZero2);
printf("giveMeAOne (clientApp): %i\n",giveMeAOne);

And the function addresses do not overlap! How can that be?

 

The library and client applications are both 32 bit, running on Ubuntu12.04 32bit in a virtualBox

0

Share this post


Link to post
Share on other sites

The linker can and will merge functions which have an identical sequence of assembly instructions into one function. This is a good thing because it reduces code size, with templates that code size reduction can be significant.

 

However, it sounds like that in this case that merging is having an unwanted side effect. You might be able to find a linker setting to switch it off. What are your linker command lines?

1

Share this post


Link to post
Share on other sites

Thank you for your explanation.

But the linker does its job just after compilation, right? then a library is generated.

In my case, with the exact same library that internally calls giveMeAZero, I can have two different scenarios:

 

1) client app also calls giveMeAZero --> when the library itself calls giveMeAZero, I get a segmentation fault

2) client app doesn't call giveMeAZero (but binds the function) --> when the library itself calls giveMeAZero, it works!

 

How can that be??

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0