Is SDL_GetRGB screwed?

Okay, I’m back on the list again. I quit for a while because somuch
email was coming in that my mail-box was kinda looking a bit flooded :frowning:

You prolly won’t remember, but I was aking a couple of months back I was
in asking questions about a pixel reading function for SDL. Anyway,after
lots of mails, and even a correspondance on the subject with Sam
Lantinga about I found that my pixel readinf function was perfectly
written and had no bugs. But my pixel read function still refused to
work. Anyway, so, I had to take a break for my school exams, but got
back on the case again a short while back. I did a bit of debugging, as
sugested to me by Julian Peterson in my original questioning thingy.
After a bit of work, I found that the source of my problems, the source
of every single seg fault the popped up was the SDL_GetRGB function!!!
I also tried writing a program that obtained a uint32 pixel value from 2
sources, 1 from my pixel read func, the other from the SDL_MapRGB
function and then running both of them through SDL_GetRGB, and not
surprisingly, both of these obtained Uint32s (Which were identical, when
I examined them) caused a seg fault! So, the question is, is there
something screwed up with SDL_GetRGB, because it sure as hell looks that
way on my system. Has anyone else tried this and had this prob, and more
importantly, does anyone know a simple fix??? (Or else I’ll have to
write a seperate buffer type class that tracks RGB values :frowning:

Anyway, as you can understand, this is kinda pissing me off :-)–
Sp1k3 Cann1bal (@Sp1k3_Cann1bal)
Pig Iron Studios
"He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…" - Frederick Nietzsche

After a bit of work, I found that the source of my problems, the source
of every single seg fault the popped up was the SDL_GetRGB function!!!
I also tried writing a program that obtained a uint32 pixel value from 2
sources, 1 from my pixel read func, the other from the SDL_MapRGB
function and then running both of them through SDL_GetRGB, and not
surprisingly, both of these obtained Uint32s (Which were identical, when
I examined them) caused a seg fault!

Can you send the debug output of the line that causes the crash?
It sounds like you are trying to dereference a Uint32 as if it were a
pointer, which would cause a crash.

See ya,
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

Sam Lantinga wrote:

After a bit of work, I found that the source of my problems, the source
of every single seg fault the popped up was the SDL_GetRGB function!!!
I also tried writing a program that obtained a uint32 pixel value from 2
sources, 1 from my pixel read func, the other from the SDL_MapRGB
function and then running both of them through SDL_GetRGB, and not
surprisingly, both of these obtained Uint32s (Which were identical, when
I examined them) caused a seg fault!

Can you send the debug output of the line that causes the crash?
It sounds like you are trying to dereference a Uint32 as if it were a
pointer, which would cause a crash.

Okay, the debugger output is pasted:

(gdb) run
Starting program /home/adam/programming/pretest

8355711

Program recieved signal SIGSEGV, Segmentation fault.
0x40043122 in SDL_GetRGB (pixel=Cannot access memory at address 0x10f.) at
SDL_pixels.c:376
376 *r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);

(gdb) continue
Continuing.
Fatal signal: Segmentation fault (SDL Parachute Deployed)

Program exited with code 0365

I don’t know whether what I’m trying to do is what you mentioned or not,
since I’m quite an amateur coder and wouldn’t know what I’m trying to do if
it comes out wrong :slight_smile:

I’ve also attached the code of my program which tests the function and then
bombs on me. Is there some compiler switch I should be using?>

See ya,
-Sam Lantinga, Lead Programmer, Loki Entertainment Software


Sp1k3 Cann1bal (@Sp1k3_Cann1bal)
Pig Iron Studios
"He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…" - Frederick Nietzsche

-------------- next part --------------
#include <iostream.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <pre.hpp>
#include <pde.hpp>

int main(void)
{
SDL_Surface *myscreen;
SDL_Event myevent;

PRE *mypre;
PDE *mypde;

Uint8 R, G, B;
Uint32 testcolour;

if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
	cerr << "\n\nThere was an error loading the basic SDL video functions!\n\n";
	exit(1);
}

atexit(SDL_Quit);

myscreen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if(myscreen == NULL)
{
	cerr << "\n\nThere was an error going into display mode 640x480x8!\n\n";
	exit(2);
}

mypre = new PRE(myscreen);
mypde = new PDE(myscreen);

testcolour = SDL_MapRGB(myscreen->format, 127, 127, 127);
cerr << endl << testcolour;
mypde->DrawPixel(SDL_MapRGB(myscreen->format, 127, 127, 127), 50, 50);
cerr << endl << mypre->ReadPixel(50,50) << endl;
// SDL_GetRGB(testcolour, myscreen->format, R, G, B);
// cerr << R << endl << G << endl << B << endl;

SDL_UpdateRect(myscreen, 0, 0, 0, 0);

while ( SDL_WaitEvent(&myevent) >= 0 ) {
             switch (myevent.type) {
                     case SDL_KEYDOWN: {
                     		delete mypre;
                     		delete mypde;
                     		SDL_FreeSurface(myscreen);
                     		exit(0);
                     }
                                     
                     case SDL_QUIT: {
                     		 delete mypre;
                     		 delete mypde;
                     		 SDL_FreeSurface(myscreen);
                             exit(0);
                     }
                     break;
             }
}

delete mypre;
delete mypde;
SDL_FreeSurface(myscreen);
exit(3);

}

*r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);
^^^

If that should be AND and operator, then you just saved some spaces in
the wrong place.
it should either be:
*r = (((pixel & fmt->Rmask) >> fmt->Rshift) << fmt->Rloss);
or
*r = (((pixel)&(fmt->Rmask)>>fmt->Rshift)<Rloss);

If the & operator is directly before a variable name, then the address
of the variable is returned, not the value.

Just don’t save too much spaces where you shouldn’t :wink:

Proff–

Florian ‘Proff’ Schulze - @Florian_Schulze
Member: TeamTNT - http://www.teamtnt.com
Homepage: - http://proff.fly.to
ICQ#: - 40510245

After a bit of work, I found that the source of my problems, the source
of every single seg fault the popped up was the SDL_GetRGB function!!!
I also tried writing a program that obtained a uint32 pixel value from 2
sources, 1 from my pixel read func, the other from the SDL_MapRGB
function and then running both of them through SDL_GetRGB, and not
surprisingly, both of these obtained Uint32s (Which were identical, when
I examined them) caused a seg fault!

I would check your usage of SDL_GetRGB, the format is:
SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r, Uint8 *g,
Uint8 *b);

So check that the fmt you’re using is good (its usually obtained from
the surface you’re reading from, eg screen->format).

And make sure that you’ve allocated space for r,g and b if you’ve
declared them as pointers (as opposed to passing the address of some
Uint8s).

Otherwise try posting the section of code thats causing the problem.

Good luck!

Julian.

-----Urspr?ngliche Nachricht-----

“He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…” - Frederick Nietzsche

Friedrich. Not Frederick. Even if you can’t pronounce it.

Dipl.Inform. Dirk-Ulrich Heise
@Dirk-Ulrich_Heise
dheise at debitel.netVon: Sp1k3 Cann1bal

Friedrich. Not Frederick. Even if you can’t pronounce it.

Maybe. Monarchs, Saints, Apostles and Other Famous People often have
their names translated. Admittedly, philosophers have to be dead for longer
than a mere century to deserve this honour.

And please don’t post HTML to the list. In fact, never post HTML anywhere.

And to prevent this from straying completely offtopic:
A simple glance at SDL_GetRGB() should convince anyone that there
is not much there that can go wrong, so any crash is most likely
a bug in the calling program.

(The one remaining problem is that RGB fields smaller than 4 bits are
not scaled correctly, but I have no plans of changing this.)

Mattias Engdeg?rd wrote:

Friedrich. Not Frederick. Even if you can’t pronounce it.

Maybe. Monarchs, Saints, Apostles and Other Famous People often have
their names translated. Admittedly, philosophers have to be dead for longer
than a mere century to deserve this honour.

Hehehehehe. Yeah.

And please don’t post HTML to the list. In fact, never post HTML anywhere.

Esp when you attach pictures and shit like that. Realy pisses the hell outta
me. :slight_smile:

And to prevent this from straying completely offtopic:
A simple glance at SDL_GetRGB() should convince anyone that there
is not much there that can go wrong, so any crash is most likely
a bug in the calling program.

That’s what I thought as well, looking at it, and a description of how the sdl
pixelformat works, but what about Prof’s hint that perhaps there’s a prob with
the placement of operator (He says it all in his email above)???>

(The one remaining problem is that RGB fields smaller than 4 bits are
not scaled correctly, but I have no plans of changing this.)


Sp1k3 Cann1bal (@Sp1k3_Cann1bal)
Pig Iron Studios
"He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…" - Frederich Nietzsche

Julian Peterson wrote:

After a bit of work, I found that the source of my problems, the source
of every single seg fault the popped up was the SDL_GetRGB function!!!
I also tried writing a program that obtained a uint32 pixel value from 2
sources, 1 from my pixel read func, the other from the SDL_MapRGB
function and then running both of them through SDL_GetRGB, and not
surprisingly, both of these obtained Uint32s (Which were identical, when
I examined them) caused a seg fault!

I would check your usage of SDL_GetRGB, the format is:
SDL_GetRGB(Uint32 pixel, SDL_PixelFormat *fmt, Uint8 *r, Uint8 *g,
Uint8 *b);

Ahamm, thats what I see as well, and what I’m working with…

So check that the fmt you’re using is good (its usually obtained from
the surface you’re reading from, eg screen->format).

Yep, getting format data from an actively working surface. Hmmmmmmmmm, don’t
know whether the surface is locked or not though?
Could this cause probs???

And make sure that you’ve allocated space for r,g and b if you’ve
declared them as pointers (as opposed to passing the address of some
Uint8s).

Nope, not using pointers for those, using normal vars…

Otherwise try posting the section of code thats causing the problem.

Well, if you take a look at the attached src file, which contains one of my
attempts to use the func, you can easily see how I’ve attempted
to use. Now, maybe something is wrong, but from what I read and see in both
the src and the docs, I’m using the func exactly as I should.>

Good luck!

Julian.


Sp1k3 Cann1bal (@Sp1k3_Cann1bal)
Pig Iron Studios
"He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…" - Frederick Nietzsche

Woops, sorry, yeah. I think that’s the name I read, but my mind musta
screwed it. Thanks for the update. :slight_smile:

And, btw, no need to get bitchy on the pronounciation. I dunno how you say
it, but I kinda say it like:

Free - Drigggg (Dragging out the ggg, as you can see. How do you true
Germanics prounonce it then?)

Dirk-Ulrich Heise wrote:

-----UrsprA 1/4 ngliche Nachricht-----

“He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…” - Frederick Nietzsche Friedrich. Not
Frederick. Even if you can’t pronounce it. Dipl.Inform. Dirk-Ulrich
Heise
hei@adtranzsig.de
dheise@debitel.netVon:Sp1k3 Cann1bal


Sp1k3 Cann1bal (@Sp1k3_Cann1bal)
Pig Iron Studios
"He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…" - Frederick Nietzsche

Yo, guys, anyone come up with any ideas and perhaps a fix to my problem
yet. You’ve all been fairly quite on the subject. Sam, I posted the
debuggger output, as you can see. Have you been able to give it a squiz
yet, and maybe indicate the prob?

Florian ‘Proff’ Schulze wrote:> > *r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);

          ^^^

If that should be AND and operator, then you just saved some spaces in
the wrong place.
it should either be:
*r = (((pixel & fmt->Rmask) >> fmt->Rshift) << fmt->Rloss);
or
*r = (((pixel)&(fmt->Rmask)>>fmt->Rshift)<Rloss);

If the & operator is directly before a variable name, then the address
of the variable is returned, not the value.

Just don’t save too much spaces where you shouldn’t :wink:

Proff

Florian ‘Proff’ Schulze - florian.proff.schulze at gmx.net
Member: TeamTNT - http://www.teamtnt.com
Homepage: - http://proff.fly.to
ICQ#: - 40510245


Sp1k3 Cann1bal (@Sp1k3_Cann1bal)
Pig Iron Studios
"He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…" - Frederich Nietzsche

Program recieved signal SIGSEGV, Segmentation fault.
0x40043122 in SDL_GetRGB (pixel=Cannot access memory at address 0x10f.) at
SDL_pixels.c:376
376 *r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);

The arguments to SDL_GetRGB(), fmt, r, g, and b must all point to valid memory.
In your program, they are either corrupted or being passed bogus paramters.
It’s not a bug in SDL.

-Sam Lantinga, Lead Programmer, Loki Entertainment Software

Uint8 R, G, B;
// SDL_GetRGB(testcolour, myscreen->format, R, G, B);
// cerr << R << endl << G << endl << B << endl;

This should be:
// SDL_GetRGB(testcolour, myscreen->format, &R, &G, &B);
// cerr << R << endl << G << endl << B << endl;

I think that’s what is causing your crash.

-Sam Lantinga, Lead Programmer, Loki Entertainment Software

Florian ‘Proff’ Schulze wrote:

*r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);
^^^
If that should be AND and operator, then you just saved some spaces in
the wrong place.
it should either be:
*r = (((pixel & fmt->Rmask) >> fmt->Rshift) << fmt->Rloss);
or
*r = (((pixel)&(fmt->Rmask)>>fmt->Rshift)<Rloss);

If the & operator is directly before a variable name, then the address
of the variable is returned, not the value.

Utterly wrong, Florian - C doesn’t care, the tokenizer spits out
the tokens “pixel”,"&",“fmt” whether they’re separated by a
space or by nothing. It’s a whitespace-insensitive language.

Dipl.Inform. Dirk-Ulrich Heise
@Dirk-Ulrich_Heise
dheise at debitel.net

Dirk-Ulrich Heise wrote:

Florian ‘Proff’ Schulze wrote:

*r = (((pixel&fmt->Rmask)>>fmt->Rshift)<Rloss);
^^^
If that should be AND and operator, then you just saved some spaces in
the wrong place.
it should either be:
*r = (((pixel & fmt->Rmask) >> fmt->Rshift) << fmt->Rloss);
or
*r = (((pixel)&(fmt->Rmask)>>fmt->Rshift)<Rloss);

If the & operator is directly before a variable name, then the address
of the variable is returned, not the value.

Utterly wrong, Florian - C doesn’t care, the tokenizer spits out
the tokens “pixel”,"&",“fmt” whether they’re separated by a
space or by nothing. It’s a whitespace-insensitive language.

Dipl.Inform. Dirk-Ulrich Heise
hei at adtranzsig.de
dheise at debitel.net

Uhh, yes. If the & would be a address operator you would get a syntax
error because it doesn’t fit to the pixel token before. And in the
second line I made another mistake, I only closed the brackets but
didn’t open them.
It was late when I wrote it :wink: The next time I will think a little bit
longer.

Proff–

Florian ‘Proff’ Schulze - @Florian_Schulze
Member: TeamTNT - http://www.teamtnt.com
Homepage: - http://proff.fly.to
ICQ#: - 40510245

Aaaaah, thankyou, thankyou. Thanks so much. I tried what you suggested
below and it worked just fine. No seg fault. I still gotta make sure
its working totally, cause output of the RGB values shot blanks at
me,but I can test forthat shit in another way. Thanks.

Hehehehe, silly me, I feel so embarrased. I’ve never really learned what
those & operators are for. My C and CPP tuts that I used weren’t exactly
supra-cool in every area, and I’m too poor to buy a book on cpp or c :frowning:
But, if I remember the tuts, they make it point to the address or
something, don’t they? Like, not the R, but the address of R? Am I
right? And this should be used whenever you want to pass a value to a
func where it accepts pointers and you’re not already passing a pointer,
like myscreen->format???

Anyway, thanks for the help. You’re a good guy.

Cheers

Sam Lantinga wrote:> > Uint8 R, G, B;

  // SDL_GetRGB(testcolour, myscreen->format, R, G, B);
  // cerr << R << endl << G << endl << B << endl;

This should be:
// SDL_GetRGB(testcolour, myscreen->format, &R, &G, &B);
// cerr << R << endl << G << endl << B << endl;

I think that’s what is causing your crash.

    -Sam Lantinga, Lead Programmer, Loki Entertainment Software


OOPMan (@OOPMan)
“He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…” - Frederich Nietzsche

Oops, looks like for some reason cerr likes Uint32’s but seems to choke
a bit on Uint8’s, hehehehe. The values came out just fine with printf
and another little test. Cool.

Sam Lantinga wrote:> > Uint8 R, G, B;

  // SDL_GetRGB(testcolour, myscreen->format, R, G, B);
  // cerr << R << endl << G << endl << B << endl;

This should be:
// SDL_GetRGB(testcolour, myscreen->format, &R, &G, &B);
// cerr << R << endl << G << endl << B << endl;

I think that’s what is causing your crash.

    -Sam Lantinga, Lead Programmer, Loki Entertainment Software


OOPMan (@OOPMan)
“He who fights with monsters should look to it that he himself
does not become a monster…When you gaze long into the abyss,
the abyss also gazes into you…” - Frederich Nietzsche