LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2016, 09:32 PM   #1
Mol_Bolom
Member
 
Registered: Nov 2008
Location: S.W. Kansas
Distribution: Slackware64 14.0 / 14.2
Posts: 245
Blog Entries: 2

Rep: Reputation: 41
SDL2 libc.so.6 __read_nocancel()


While converting some SDL2 code to c from Lazy Foo 's c++ tutorials I came across a rather odd problem and am rather curious as to the reason for it.

gcc-4.7.1_multilib-x86_64-1alien
glibc-2.15_multilib-x86_64-8alien


Code:
void close(void)
{
     SDL_FreeSurface(img);
     SDL_DestroyWindow(window);
     SDL_Quit();
}
/*  If these lines of code are in a separate function, such as close(), rather than in main(),
 *  the program goes into an infinite loop, or freezes to which gdb 
 *  gives the following after breaking from it.
 *
 * 0x00007ffff77fc270 in __read_nocancel () from /lib64/libc.so.6
 *
 * However, if I place the three lines at the end of the main() 
 * replacing the call to close() there's no problem and the 
 * examples run smoothly.
*/
int main(int argc, char **argv)
{
\*
 * 
 * Writing it this way works just fine.
 * 
 *\

     SDL_FreeSurface(img);
     SDL_DestroyWindow(window);
     SDL_Quit();

     return(0);
}
Anyway, I've solved it, so that's not my question. I just want to understand why things like this happen, because I've come across several weird oddities that I just can't figure out why.

Anyway, thanks for any help.

<Edit>

Well, while going through the assembly in gdb, and reviewing the code once again. I see the hang happens with the call to SDL_Init, for some such reason. From what little I can gather, it's either a compiler problem with gcc, or a libc problem.

Alrighty, a second test, I removed close() from within the single source file and wrote it in a separate file. Compiled both to objects, then linked them. Set a breakpoint for the function closs() and lo and behold, the program somehow skips out of the init() function at the call to SDL_Init and goes directly to the close() function.

Code:
#0  close (win=0xe, im=0x2) at close.c:9
#1  0x00007ffff68a39fe in xcb_disconnect () from /usr/lib64/libxcb.so.1
#2  0x00007ffff6ad7827 in XCloseDisplay () from /usr/lib64/libX11.so.6
#3  0x00007ffff7b9020f in ?? () from /usr/lib64/libSDL2-2.0.so.0
#4  0x00007ffff7b81983 in ?? () from /usr/lib64/libSDL2-2.0.so.0
#5  0x00007ffff7aea58f in ?? () from /usr/lib64/libSDL2-2.0.so.0
#6  0x0000000000400c34 in init () at 05-optimize.c:35
#7  0x0000000000400e33 in main (argc=1, argv=0x7fffffffdff8) at 05-optimize.c:121
And in the other program where it's all in one source,same thing.

And when I move the function to the beginning of the file,

Code:
#0  0x00007ffff77fc270 in __read_nocancel () from /lib64/libc.so.6
#1  0x00007ffff502f465 in ?? () from /usr/lib64/libdbus-1.so.3
#2  0x00007ffff5030289 in ?? () from /usr/lib64/libdbus-1.so.3
#3  0x00007ffff5031da2 in ?? () from /usr/lib64/libdbus-1.so.3
#4  0x00007ffff5027564 in ?? () from /usr/lib64/libdbus-1.so.3
#5  0x00007ffff5027405 in ?? () from /usr/lib64/libdbus-1.so.3
#6  0x00007ffff501234b in ?? () from /usr/lib64/libdbus-1.so.3
#7  0x00007ffff500ed88 in ?? () from /usr/lib64/libdbus-1.so.3
#8  0x00007ffff7b94e52 in ?? () from /usr/lib64/libSDL2-2.0.so.0
#9  0x00007ffff7b908fd in ?? () from /usr/lib64/libSDL2-2.0.so.0
#10 0x00007ffff7b817e1 in ?? () from /usr/lib64/libSDL2-2.0.so.0
#11 0x00007ffff7aea58f in ?? () from /usr/lib64/libSDL2-2.0.so.0
#12 0x0000000000400c5d in init () at 05-optimize.c:42
#13 0x0000000000400e5c in main (argc=1, argv=0x7fffffffdff8) at 05-optimize.c:121
Woops, it's getting late. Either way, interesting. Still have no idea what's going on, meh, I'll figure it out.

Just for completion's sake. The working code only shows this when I break at the first call to SDL_FreeSurface, SDL_DestroyWindow, or SDL_Quit (Doesn't matter which one I put in a function, all hang the same).

Code:
#0  main (argc=1, argv=0x7fffffffdff8) at 05-optimize.c:124
And the full code.

Code:
#include <stdio.h>
#include <SDL2/SDL.h>
#include <string.h>



SDL_Surface *window = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *img = NULL;

//SDL_Surface *stretchedimg = NULL;

enum USE_SDL_GETERROR
{
        USE_GETERROR,
        NOUSE_GETERROR
}; 

void pError(char *msg, int userr)
{
        if(userr == USE_GETERROR)
                printf(msg, SDL_GetError());
        else
                printf(msg);
}

int init(void)
{
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
        {
                pError("SDL could not initialize! error: %s\n", USE_GETERROR);
                return(1);
        } else {
                window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
                if(window == NULL)
                {
                        pError("Window could not be created! error: %s\n", USE_GETERROR);
                        return(1);
                } else {
                        screen = SDL_GetWindowSurface(window);
                }
        }
        return(0);
}

SDL_Surface *loadSurface(char *imgfile)
{
        SDL_Surface *optimizedSurface = NULL;

        SDL_Surface *loadedimg = SDL_LoadBMP(imgfile);
        if(loadedimg == NULL)
        {
                pError("Unable to load image! error: %s\n", USE_GETERROR);
        } else {
                optimizedSurface = SDL_ConvertSurface(loadedimg, screen->format, NULL);
                if(optimizedSurface == NULL)
                {
                        pError("Unable to optimize image! error: %s\n", USE_GETERROR);
                }
                SDL_FreeSurface(loadedimg);
        }
        return(optimizedSurface);
}

int loadimg(char *imgfile)
{
        img = loadSurface(imgfile);
        if(img == NULL)
        {
                pError("Failed to load image!\n", NOUSE_GETERROR);
                return(1);
        }
        return(0);
}




void run(void)
{
    SDL_Event e;

    while(1)
    {
        while(SDL_PollEvent(&e) != 0)
        {
            if((e.type ==  SDL_QUIT) || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE))
                    return;
        }

        SDL_Rect stretchRect;
        stretchRect.x = 0;
        stretchRect.y = 0;
        stretchRect.w = 640;
        stretchRect.h = 480;
        SDL_BlitScaled(img, NULL, screen, &stretchRect);

        SDL_UpdateWindowSurface(window);

    }


}

#ifdef __USE_CLOSE
                                      //  This causes the hang.
void close(void)
{

        SDL_FreeSurface(img);
        SDL_DestroyWindow(window);
        SDL_Quit();
}
#endif

int main(int argc, char **argv)
{
        if(init() == 1)
        {
                printf("Failed to initialize!\n");
        } else {
                if(loadimg("stretch.bmp") == 1)
                {
                        printf("Failed to load image!\n");
                } else {
                        run();
                }
        }
#ifdef __USE_CLOSE
        close();                      // This is the other part that causes the hang.
#else
        SDL_FreeSurface(img);           // These three lines compile and run just fine.
        SDL_DestroyWindow(window);
        SDL_Quit();
#endif
        return(0);
}
Pshhhh! I'll figure it out...

Last edited by Mol_Bolom; 05-29-2016 at 01:02 AM.
 
Old 05-29-2016, 12:46 PM   #2
Mol_Bolom
Member
 
Registered: Nov 2008
Location: S.W. Kansas
Distribution: Slackware64 14.0 / 14.2
Posts: 245

Original Poster
Blog Entries: 2

Rep: Reputation: 41
Well spray me with two pounds of potted meat. It was the name close(). Changed it, and voila, it works now.

man close only finds a reference to unistd.h, so not sure how, but I presume it got added without including unistd.h. Either way, next time this happens, hopefully I'll remember that it happens when you use the same name of another function, and of course gcc won't throw any errors about it.

Last edited by Mol_Bolom; 05-29-2016 at 12:48 PM.
 
Old 05-29-2016, 11:45 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Alsó make your funtions 'static' by default.
 
Old 05-30-2016, 02:20 AM   #4
Mol_Bolom
Member
 
Registered: Nov 2008
Location: S.W. Kansas
Distribution: Slackware64 14.0 / 14.2
Posts: 245

Original Poster
Blog Entries: 2

Rep: Reputation: 41
Static functions. Don't know how things like this happen, but your text had me search it up and something finally clicked. So I now have a decent comprehension about static functions now. One of the few things I have had a hard time understanding.

Anyway, I'll have to write some example programs and look over the assembly to see if I have that understood right. One more thing to add to my todo list while translating these tutorials.

Also, looking over writing C with C++ I came across "Name Mangling". I've heard that term before, but never truly understood it. Anyhoo, from what I gather if I compiled the "unmodified" code in C++, the function close() would be mangled to unique name, so it wouldn't get "confused" with the other close() function. However, in C, it doesn't do that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to intall sdl2 without dependencies hell? siery Linux - Newbie 5 12-31-2017 03:51 AM
[SOLVED] Need SDL2 tutorial and how to I scale textures ? metaschima Programming 2 05-10-2014 11:25 AM
LXer: SDL2.0 Release Is Now Out For Developers LXer Syndicated Linux News 0 08-14-2013 05:02 AM
SDL2 Released dugan Linux - News 1 08-13-2013 02:49 PM
Perl SDL2 install problem vargadanis Linux - Software 0 10-22-2006 01:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration