Error msg

When i try to run my “game” i get a error msg "Fatal Signal: Segmentation
fault " I dont get any error when I compile it. I
use MSVC++ 6.0. I have included my source code, if thats might help.

//Fredrik “Perra” Persson

-------------------------------my-code-sample-------------------------------------------
#include <stdio.h>
#include <stdlib.h>

#include <SDL.h>

SDL_Surface *back;
SDL_Surface *sprite;
SDL_Surface *screen;

int xpos=0,ypos=0; //spritens start pos

/////////////////////////////////////////////////////
//h?r laddar vi bilderna
/////////////////////////////////////////////////////

int InitImages()
{
back=SDL_LoadBMP(“back.bmp”);
sprite = SDL_LoadBMP(“sprite.bmp”);
return(0);
}

/////////////////////////////////////////////////////
//funktioner f?r att rita ut r?tt bild p? r?tt st?lle
/////////////////////////////////////////////////////

//ritar hela src-img till dest-img
void DrawImage(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}

//denna funktion ritar en del av src-img till dest-img
void DrawImage(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}

//ritar bara bakgrunden
void DrawBG()
{
DrawImage(back, 0,0);
}

//funktionen f?r att skapa det som ska visas p? sk?rmen
void DrawScene()
{
//anv?nder den avancerade varianten av DrawImage
DrawImage(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
//anv?nder den enkla varianten
DrawImage(sprite, xpos, ypos);

//Flippar backbuffern, till sk?rmen
SDL_Flip(screen);
}

/////////////////////////////////////////////////////
//this is the main part of the program
/////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
//Uint8* keys; //ett perverst behov f?r att anv?nda tagentbordet?

if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
int atexit (SDL_QUIT);

//Skapa surface 640X680X16bbp
SDL_Surface* pSurface = SDL_SetVideoMode(640, 480, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);

//anropar respektive funktion
InitImages();
DrawBG();

//Deklarera en event
SDL_Event event;

for (;:wink:
{
if (SDL_PollEvent (&event))
{
if (event.type == SDL_QUIT) break;
}
//anrop till funktion
DrawScene();

}

return (0);
}
-----------------------------------------------end of
code--------------------------------

When i try to run my “game” i get a error msg "Fatal Signal: Segmentation
fault " I dont get any error when I compile it. I
use MSVC++ 6.0. I have included my source code, if thats might help.

It would be more helpful if you could isolate exactly where the crash
occurs. An easy way to do this is to sprinkle various printf() statements
through the code so you know where it’s up to; or just run it under MSVC’s
debugger :wink:

SDL_Surface *sprite;
SDL_Surface *screen;

int xpos=0,ypos=0; //spritens start pos

int InitImages()
{
back=SDL_LoadBMP(“back.bmp”);
sprite = SDL_LoadBMP(“sprite.bmp”);

You should check the return value of SDL_LoadBMP() - if it returns NULL,
your app will crash when you try to blit the surface, or do anything
else with it. If it does return NULL, you can find out why with
SDL_GetError().

return(0);
}

//funktionen f?r att skapa det som ska visas p? sk?rmen
void DrawScene()
{
//anv?nder den avancerade varianten av DrawImage
DrawImage(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);

This could cause problems - on the first cycle, xpos and ypos are both
0. I’m not sure when SDL_BlitSurface() does when you ask it to blit from
a negative coordinate on the source rectangle.

//anv?nder den enkla varianten
DrawImage(sprite, xpos, ypos);

//Flippar backbuffern, till sk?rmen
SDL_Flip(screen);
}

int main(int argc, char* argv[])
{
//Uint8* keys; //ett perverst behov f?r att anv?nda tagentbordet?

if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
int atexit (SDL_QUIT);

I’m not sure exactly what this means to the compiler… but that int
shouldn’t be there. Also, it’s SDL_Quit, not SDL_QUIT - the former being
the cleanup function, the latter being an event indicating that the user
wants to quit.

//Skapa surface 640X680X16bbp
SDL_Surface* pSurface = SDL_SetVideoMode(640, 480, 32,
SDL_HWSURFACE|SDL_DOUBLEBUF);

You should probably check that this succeeds too…

//anropar respektive funktion
InitImages();
DrawBG();

//Deklarera en event
SDL_Event event;

for (;:wink:
{
if (SDL_PollEvent (&event))
{
if (event.type == SDL_QUIT) break;
}
//anrop till funktion
DrawScene();
}

Hm… you never seem to free the images you loaded. This won’t cause a
crash, but it’s bad practice not to free memory you allocate in a
program.

return (0);
}

If none of my suggestions help, try to pin down where the crash is occurring
and ask again.On Mon, Feb 04, 2002 at 07:16:59PM +0100, Fredrik Persson wrote:


Mike.
You read it, you can’t unread it.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20020204/ee6029dc/attachment.pgp

Mike wrote:

When i try to run my “game” i get a error msg "Fatal Signal: Segmentation
fault " I dont get any error when I compile it. I
use MSVC++ 6.0. I have included my source code, if thats might help.

It would be more helpful if you could isolate exactly where the crash
occurs. An easy way to do this is to sprinkle various printf() statements
through the code so you know where it’s up to; or just run it under MSVC’s
debugger :wink:
Here is a little .h file that I have been using in various forms since,
oh, around 1982…> On Mon, Feb 04, 2002 at 07:16:59PM +0100, Fredrik Persson wrote:


////////////////////////////////////////////////////////////////
//
// trace.h
//
// Copyright given to the public domain
//
////////////////////////////////////////////////////////////////

#ifndef TRACE_H
#define TRACE_H

#ifdef __cplusplus
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>

#define trace() (cout << (FILE) << “@” << (LINE) << endl <<
flush);
#else
#include <stdio.h>
#include <stdlib.h>
#define trace() printf("%s@%d\n", FILE, LINE);
#endif

#endif

Just put trace(); anywhere in your C/C++ code and everytime that line is
reached it will print the file name and line number where the trace();
is. This is very handy when you have a LOOOOONNG run from start to error
and you want to know where to set that first breakpoint.

	Bob P.

You never initialize ‘screen’, as far as I can see.

Good luck,
Willem JanOn Mon, 2002-02-04 at 19:16, Fredrik Persson wrote:

When i try to run my “game” i get a error msg "Fatal Signal: Segmentation
fault " I dont get any error when I compile it. I
use MSVC++ 6.0. I have included my source code, if thats might help.

//Fredrik “Perra” Persson

-------------------------------my-code-sample-------------------------------

#include <stdio.h>
#include <stdlib.h>

#include <SDL.h>

SDL_Surface *back;
SDL_Surface *sprite;
SDL_Surface *screen;

Bob Pendleton wrote:
Here is a little .h file that I have been using in various forms
since,
oh, around 1982…
[snip comment block]

#ifdef __cplusplus
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>

#define trace() (cout << (FILE) << “@” << (LINE) << endl <<
flush);
#else
#include <stdio.h>
#include <stdlib.h>
#define trace() printf("%s@%d\n", FILE, LINE);
#endif

#endif

In 1982 this was fine, but now you’ll probably want std::cout and
std::endl in the C++ macro. Also it would probably be better to include
and rather than <stdio.h> and <stdlib.h> in the C++
version. If you want fstream, it should be , but, you probably
don’t actually want that. It is built on , and you’re not
using anything in . contains the declarations for
cout and cin.

The C looks good though. :slight_smile:

HTH,

S-

Steve Salkin wrote:

Bob Pendleton wrote:
Here is a little .h file that I have been using in various forms
since,
oh, around 1982…
[snip comment block]

#ifdef __cplusplus
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>

#define trace() (cout << (FILE) << “@” << (LINE) << endl <<
flush);
#else
#include <stdio.h>
#include <stdlib.h>
#define trace() printf("%s@%d\n", FILE, LINE);
#endif

#endif

In 1982 this was fine, but now you’ll probably want std::cout and
std::endl in the C++ macro. Also it would probably be better to include
and rather than <stdio.h> and <stdlib.h> in the C++
version. If you want fstream, it should be , but, you probably
don’t actually want that. It is built on , and you’re not
using anything in . contains the declarations for
cout and cin.

You are absolutely right, of course, the only thing you got wrong is
that the C++ part wouldn’t have worked at all in 1982 as there was no
generally available C++ at that time. My first edition of Stroustrup’s
"The C++ programming language" has a copyright date of 1986. :slight_smile: I used
to have the issue of SIGPLAN plan notices that had the original C++
article, lost it in a move about 12 years ago.

	Bob P.-- 

±-----------------------------------+

  • Bob Pendleton is seeking contract +
  • and consulting work. Find out more +
  • at http://www.jump.net/~bobp +
    ±-----------------------------------+

Bob Pendleton wrote:

Bob Pendleton wrote:
Here is a little .h file that I have been using in various forms
since, oh, around 1982…
[snip]

In 1982 this was fine, but now you’ll probably want std::cout and
std::endl in the C++ macro. Also it would probably be better to
include and rather than <stdio.h> and <stdlib.h>
in the C++ version. If you want fstream, it should be ,
but, you probably don’t actually want that. It is built on
, and you’re not using anything in .
contains the declarations for cout and cin.

You are absolutely right, of course, the only thing you got wrong is
that the C++ part wouldn’t have worked at all in 1982 as there was no
generally available C++ at that time. My first edition of Stroustrup’s
"The C++ programming language" has a copyright date of 1986. :slight_smile: I
used to have the issue of SIGPLAN plan notices that had the original
C++ article, lost it in a move about 12 years ago.

According to Stroustrup the term “C++” was first used to describe “C
with Classes” in Dec 1983. It then described a new object-based C
language that had been under development for over four years.(1)

I knew it was around then that C++ first became available, and your
comment Bob seemed to suggest that you were around and programming in
those days. You can’t blame me for taking you at your word. :slight_smile: Besides
which, the first part of your macro must not have been very useful if
you didn’t have a C++ compiler. :slight_smile:

(1) The Design and Evolution of C++, Stroutrup, 1994 pp 1-64; "C++"
chosen on p. 64.

S-

Steve Salkin wrote:

Bob Pendleton wrote:

Bob Pendleton wrote:
Here is a little .h file that I have been using in various forms
since, oh, around 1982…
[snip]

In 1982 this was fine, but now you’ll probably want std::cout and
std::endl in the C++ macro. Also it would probably be better to
include and rather than <stdio.h> and <stdlib.h>
in the C++ version. If you want fstream, it should be ,
but, you probably don’t actually want that. It is built on
, and you’re not using anything in .
contains the declarations for cout and cin.

You are absolutely right, of course, the only thing you got wrong is
that the C++ part wouldn’t have worked at all in 1982 as there was no
generally available C++ at that time. My first edition of Stroustrup’s
"The C++ programming language" has a copyright date of 1986. :slight_smile: I
used to have the issue of SIGPLAN plan notices that had the original
C++ article, lost it in a move about 12 years ago.

According to Stroustrup the term “C++” was first used to describe “C
with Classes” in Dec 1983. It then described a new object-based C
language that had been under development for over four years.(1)

I knew it was around then that C++ first became available, and your
comment Bob seemed to suggest that you were around and programming in
those days. You can’t blame me for taking you at your word. :slight_smile: Besides
which, the first part of your macro must not have been very useful if
you didn’t have a C++ compiler. :slight_smile:

I don’t blame you, and I expect you to take me at my word. I got my
first paid programming job in 1974 and had been studying programming for
a while before that. The key phrase that addresses the usability of the
.h file in the early eighties is the part where is said “in various
forms”. The C++ part was added in the early '90s, the C part dates to
the early '80s. I have a version that works in Java too, though it is
much more complex in Java due to the lack of a preprocessor. The Java
version is just a year old now. I got into the habit of using trace
macros when I was doing a lot of 8080 assembly hacking in the '70s and
early '80s.

(1) The Design and Evolution of C++, Stroutrup, 1994 pp 1-64; "C++"
chosen on p. 64.

Good book, I highly recommend it to anyone who wants to understand C++,
students of language design, and history buffs.

	Bob Pendleton> 

S-


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


±-----------------------------------+

  • Bob Pendleton is seeking contract +
  • and consulting work. Find out more +
  • at http://www.jump.net/~bobp +
    ±-----------------------------------+