PDA

View Full Version : What's a SIGSEGV exception?



IlovePascal
02-07-2007, 01:29 PM
System : Windows XP
Compiler: Lazarus (most recent version)
Libraries: OpenGL

Hello, I have written a very simple program in Lazarus using OpenGL and the LazOpenGLbox that someone posted here on PGD a while ago, but every time I try to compile+run it, it comes up with a message box saying something about an External SIGSEGV error...

What should I do? Where does it come from?

thanks for your help!

Setharian
02-07-2007, 01:39 PM
SIGSEGV = Segmentation Fault = Access Violation under Win

most probably you are accessing invalid memory location....

WILL
02-07-2007, 10:21 PM
Common causes are;

:arrow: miscalculating array ranges in your loops,
:arrow: accessing objects when not created,
:arrow: or just plain and simple trying to access memory that was not allocated for use. (ie. a texture that didn't load --though in OpenGL this will hang your app. :?)

IlovePascal
04-07-2007, 12:48 PM
Ok, I understand where my mistake is now.

However, as Im learning the very basics of Object Orientated Pascal with all the classes and stuff, I've been learning from other programs that I found online and I try to understand how it works.

I obviously haven't managed to initialize my object properly since the error occurs everytime I try to access a variable from that class.
Here is a copy of my program:
http://files-upload.com/347054/DrawerNtWkng.rar.html
Could someone have a look (the unit having problems is 'Background') and tell me what I'm not doing properly, please?
Is it the way I do my properties? What's the proper way of doing it? Or is it just the way I initialize my variables?

Thanks a lot!

WILL
04-07-2007, 01:57 PM
Hi ILP,

Object Pascal does take a second set of understanding on top of knowing Pascal it's self. But the good news is that it'll be worth it once you've 'gotten it' and it'll be just like riding a bicycle. ;)

Some cardinal rules of thumb are:

1) always allocate your objects before you try to use them.
2) free (or deallocate) EVERYTHING you create (or allocate) or you'll have memory leaks in your program each time you don't.
3) if you want to access procedures, functions, properties, methods or variables from OUTSIDE your object, be sure to make them public.

#3 is done by default though so if you don't specify public or private in your object class OR anything you put above where you put private will automatically be public. This is mostly for design time and organizational purposes, it means nothing to the end user. :)

Checking your source... (btw, it is sometimes best if you can post your code in the forums. Provided that it's not too big or it's directly relevant to the direct problem. And again, sometimes not. :))

WILL
04-07-2007, 02:22 PM
Ok, had a quick look.

The first problem I see is that you seem to have copied over your lpr file. :? It looks just like the Background.pas unit.

Background.pas doesn't show me anything useful. Heres why:

1) I cannot see how you are creating or freeing your objects.

Your constructor and destructor are fine. You could alternately do the following...
type
TMyClass = class(TObject)
end;
...and your Create and Free constructors would already exist due to the fact that it's a descendant of the base class 'TObject', which is handy as it has that basic functionality already.

The truth is it doesn't really matter what you do inside of those two though. You can have a 'Hello world!' program in there, once it's done executing it'll create or destroy the object.

Creating your own constructor or destructor is only useful if you have something extra to do such as assigning some basic values to variables or freeing allocated memory from within your object. Those are common uses, but there may be more obviously.

The best that I can do at this point is show you in short how to properly create and free an object you have already declared.


// Declare
type
TSimpleObject = class(TObject)
end;
var
MyObject: TSimpleObject;

begin
// Create
MyObject := TSimpleObject.Create;

// Destroy
MyObject.Free;


That all there is to it really.

Why can't you just call 'MyObject.Create'? Well because MyObject is just a pointer not the actual object so you have to tell it what it belongs to before it can do anything. Since TSimpleObject is where all the function code is, only after you have pointed MyObject to it will it be able to properly execute the code.

On that same principle 'MyObject.Free' will work just fine because it already is pointing to where it needs and is fully allocated.


Hope this helps. If not then let us know and we'll do our best to clear up any fuzzy spots. ;)

IlovePascal
13-07-2007, 06:53 PM
Hey! Thank you so much for this very comprehensive reply!
Now I know what I was doing wrong. I wasn't doing
background := TBackground.Create
but simply
background.Create
!
Thanks again, great reply! ;)

WILL
13-07-2007, 07:14 PM
np ;)