Page 1 of 1

Segmentation Fault? What's that mean?

Posted: Mon Mar 20, 2006 10:58 pm
by sabaal
I'm getting an error from my program called "segmentation fault". I'm very new to C++ and Irrlicht, so I don't know what that means, or if it's coming from the engine, or from something else in my program, or what. I have narrowed down the cause to around my tcySprite class, but I'm not sure exactly where it's happening.

Most of the code I'm using consists of alterations to the 2D graphics demo included with Irrlicht. I have added one class:

Code: Select all

class tcySprite{
	public:
		IVideoDriver* SDriver;
		ITexture* ImageSource;
		rect<s32> frame[];
		int FrameCount;
		void DrawSprite(int XIn, int YIn, int FrameIn){
			SDriver->draw2DImage(ImageSource, position2d<s32>(XIn, YIn),
					     frame[FrameIn], 0, 
					    SColor(255,255,255,255), true);
		}
};
... and one function:

Code: Select all

tcySprite* MakeSprite(IVideoDriver* SDriverIn, ITexture* ImageSourceIn, int FrameWidthIn, int FrameHeightIn, int RowIn, int FrameCountIn){
	tcySprite* SpriteOut;
	SpriteOut -> SDriver = SDriverIn;
	SpriteOut -> ImageSource = ImageSourceIn;
	for (int i = 0; i < FrameCountIn; i += 1){
		SpriteOut -> frame[i] = rect<s32>((FrameWidthIn*i), (FrameHeightIn*RowIn), (FrameWidthIn*i)+(FrameWidthIn-1), (FrameHeightIn*RowIn)+(FrameHeightIn-1));
	}
	return SpriteOut;
}
The class is supposed to be an animated sprite, with an array of rectangles referring to regions of a texture to use as animation frames. It has a function to draw itself, which I have had problems with before, but if it acts up again I'll make a separate thread for it. For now, I have no way to test it.

The MakeSprite function seems to be what's causing the problem. Sticking "tcySprite* SpriteName" in my program isn't giving me any problems, but setting it equal to what MakeSprite returns gives me the segmentation fault error.

Here's how I'm using it:

Code: Select all

ITexture* TextureShira = driver->getTexture("./media/shira.png");

//Allows the program to continue:
tcySprite* SpriteShira01;

//Generates a segmentation fault:
tcySprite* SpriteShira02 = MakeSprite(driver, TextureShira, 32, 64, 0, 12);
And the program's output when I run it:

Code: Select all

Please select the driver you want for this example:
 (a) Direct3D 9.0c
 (b) Direct3D 8.1
 (c) OpenGL 1.5
 (d) Software Renderer
 (e) Apfelbaum Software Renderer
 (f) NullDevice
 (otherKey) exit

Automatically chosen: OpenGL [b](I told it to do this.)[/b]
Irrlicht Engine version 0.14.0
Linux Linux 2.6.12-10-386 #1 Sat Mar 11 16:13:17 UTC 2006
Creating X window...
libGL warning: 3D driver claims to not support visual 0x24
libGL warning: 3D driver claims to not support visual 0x28
libGL warning: 3D driver claims to not support visual 0x2c
libGL warning: 3D driver claims to not support visual 0x30
Using renderer: OpenGL 1.5
OpenGL driver version is 1.2 or better.
Multittexturing active.
Loaded texture: #DefaultFont
Loaded texture: ./media/shira.png
Segmentation fault
(The four "libGL warning" lines are generated every time I run the program, fault or not, but seem to pose no problem. I don't know what those mean, either.)

As you can see above, I'm using Linux 2.6, along with OpenGL. In case it somehow matters, I'm editing my code in Kdevelop, and using whatever default options it has to compile. I would try the debugger, but I have no idea how it works.

So there it is. My overcomplicated explanation of a problem which is probably as simple to fix as adding a comma somewhere. Again, I'm very new to C++, and this is the first project I've worked on using Irrlicht, so please make your explanations as soft and round as possible. That said, any kind of help is appreciated, and Technocracy's credits has plenty of empty space in the "Special Thanks" section. :wink:

Thanks in advance.

Posted: Mon Mar 20, 2006 11:37 pm
by Warchief
It is probably an invalid memory access.

Quick look at the code,

Code: Select all

... MakeSprite(...) 
{
  tcySprite* SpriteOut;
  SpriteOut->SDriver = SDriverIn;
  ...
}
Are you using SpriteOut without reserving memory?

(Probably yo missed a "new" or better using it as parameter)

Code: Select all

void MakeSprite( params, tcySprite & SpriteOut ) {
  SpriteOut.SDriver = SDriverIn; 
  ...
}

Posted: Mon Mar 20, 2006 11:39 pm
by Guest
A segmentation fault means, in general, that you're doing something nasty with memory.

Code: Select all

tcySprite* SpriteOut;
SpriteOut -> SDriver = SDriverIn; 
In this case it's these two lines. When you start filling that tcySprite object that SpriteOut points to, it hasn't been allocated any memory yet. The SpriteOut pointer is pointing to god knows what.

So, you have to first make an instance of your class by doing something like (sorry, didn't see a constructor there so I can't be specific)

Code: Select all

tcySprite* SpriteOut;
SpriteOut = new tcySprite();
That "new = blah" line reserves the memory and calls the classes constructor[. Then you can start using it. More about classes and constructors you can Google.

Posted: Mon Mar 20, 2006 11:59 pm
by sabaal
Well, that explains the problem. :roll:
Thanks. :)

But now I'm getting something different:

Code: Select all

Please select the driver you want for this example:
 (a) Direct3D 9.0c
 (b) Direct3D 8.1
 (c) OpenGL 1.5
 (d) Software Renderer
 (e) Apfelbaum Software Renderer
 (f) NullDevice
 (otherKey) exit

Automatically chosen: OpenGL
Irrlicht Engine version 0.14.0
Linux Linux 2.6.12-10-386 #1 Sat Mar 11 16:13:17 UTC 2006
Creating X window...
libGL warning: 3D driver claims to not support visual 0x24
libGL warning: 3D driver claims to not support visual 0x28
libGL warning: 3D driver claims to not support visual 0x2c
libGL warning: 3D driver claims to not support visual 0x30
Using renderer: OpenGL 1.5
OpenGL driver version is 1.2 or better.
Multittexturing active.
Loaded texture: #DefaultFont
Loaded texture: ./media/shira.png
*** glibc detected *** corrupted double-linked list: 0x0857e4b0 ***
Aborted
Yeah, I get scared when I see the word "corrupted". And I have no idea what a double-linked list is, either. All I did was add that "new" line:

Code: Select all

tcySprite* SpriteOut;
SpriteOut = new tcySprite();
SpriteOut -> SDriver = SDriverIn;
etc...
Is this coming from the same problem, or do I need a new thread for it?

Once again, thanks in advance for what I'm sure will be a very simple, "duh!" type of solution.

Posted: Tue Mar 21, 2006 12:30 am
by Guest
sabaal wrote:Once again, thanks in advance for what I'm sure will be a very simple, "duh!" type of solution.
Well, you're not allocating any memory for your list of frames.

Code: Select all

rect<s32> *frame;
frame = new rect<s32>[FrameCountIn];
I changed the "frame[]" to "*frame" because GCC didn't like the first.

That code reserves memory for FrameCountIn instances of rect. Then you can use them. The same thing essentially than earlier. Don't know 100% if this fixes your error but its a start.

Then, at the end of your program, preferably in the destructor of tcySprite, you have to delete the memory allocated to the rects. You really should google for a tutorial on "dynamic memory c++" or something like that.

Posted: Tue Mar 21, 2006 12:36 am
by sabaal
Thanks a lot. I'll do that. :)