general protection fault

DOS specific questions.
nedman47
Posts: 62
Joined: Dec 05, 2006 15:35

general protection fault

Postby nedman47 » Dec 05, 2006 15:44

I am trying to write a program that uses a linked list as a data structure. I am using callocate and pointers. The program runs sometimes and gives a GPF sometimes. A dump of the data structure shows it is linked properly. It is not easy to diagnose a GPF since it cannot be trapped.
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Postby sir_mud » Dec 05, 2006 15:49

Have your tried compiling with -exx? Sometimes it will point you in the write direction.
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Postby DrV » Dec 05, 2006 17:20

You should be able to gain more insight into the problem by compiling with -g and running in gdb (see readme.txt). Also try -exx as suggested above.
Bunuel66
Posts: 76
Joined: May 19, 2006 19:56

Linked list GFP

Postby Bunuel66 » Dec 05, 2006 17:33

Even if the list is correctly linked it could be due to a 'wild' access with an erroneous pointer.....

Regards.

Luis
nedman47
Posts: 62
Joined: Dec 05, 2006 15:35

general protection fault

Postby nedman47 » Dec 05, 2006 20:16

I compiled my program with -g switch and ran in GDB. It failed immediately with this message ...

SIGSEGV segmentation fault
0x00009dac in free()
nedman47
Posts: 62
Joined: Dec 05, 2006 15:35

general protection fault

Postby nedman47 » Dec 05, 2006 20:25

ok i figured out how to operate the debugger

the program does not fail every time

when it does fail, it is at a line like this ...

type cell
as string ptr value
as cell ptr prevcell
as cell ptr nextcell
end type
...
btm = callocate(len(cell))
btm->value = callocate(3)
*btm->value= "btm"
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » Dec 05, 2006 20:37

The way you have a String ptr, I don't think you can do that in the way you want, String is actually a Type, that is transparent in use, so its better to use a ZString ptr

My suggestion would be to do this,

Code: Select all


type cell
as ZString ptr value
as cell ptr prevcell
as cell ptr nextcell
end type
...
Dim somestring As String
somestring = "foo"
btm = callocate(sizeof(cell))
btm->value = Allocate(Len(somestring) + 1)
*btm->value = somestring

Print *btm->value


Note the +1 in order to store the null byte that marks the end of the string.

If your string will contain any chr(0), this will fail, and you'd have to do a method like this:

Code: Select all

type cell
as Any ptr value
as Integer value_len
as cell ptr prevcell
as cell ptr nextcell
end type
...
Dim somestring As String
somestring = "foo"
btm = callocate(sizeof(cell))
btm->value_len = Len(somestring)
btm->value = Allocate(btm->value_len)
memcpy(btm->value, strptr(somestring), btm->value_len)

' Then to get it back...

Dim temp As String

temp = Space(btm->value_len)
memcpy(strptr(somestring), btm->value, btm->value_len)

Print temp


This way has the advantage that it will not matter if the string contains chr(0), which is the end of string marker in a zstring.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Dec 06, 2006 3:15

Nah, you can callocate and use strings, the problem is you're not allocating enough, you're only allocating 3 bytes.

Code: Select all

btm->value = callocate(3)


needs to become

Code: Select all

btm->value = callocate(len( string ))


EDIT: oops i was mixed up in my original post ;)
nedman47
Posts: 62
Joined: Dec 05, 2006 15:35

Postby nedman47 » Dec 31, 2006 17:44

I was able to use regular strings by allocating len(text) + 12 bytes for the descriptor which is the value of sizeof(string).
counting_pine
Site Admin
Posts: 6211
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Dec 31, 2006 17:58

You don't need to allocate space for anything more than just the descriptor. When you give the string a value, then FB will allocate memory for the string's data elsewhere, and use that.

When you deallocate the string's descriptor later, you should empty the string by setting it to "" first. This will make FB deallocate the memory taken up by the string's data. Otherwise, you won't be able to deallocate it later, and you'll have a memory leak.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » Dec 31, 2006 18:12

fbc wrote:error 172: The NEW operator cannot be used with strings in 'dim x as string ptr = new string'


<< mr. smartpants :P

too bad such limitation exists though.. :/
i'm sure it'd be a cool/much cleaner feature, and not too complicated to add IMO ;)
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Dec 31, 2006 20:52

Unless the STRING type become a real object, that may not be simple to support, making it an object isn't a simple task, due the quirks that are handled by the compiler internally, unfortunately.

If it was a class, the users could choose between left( foo, 1 ) and foo.left( 1 ) etc.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » Dec 31, 2006 21:04

v1ctor wrote:Unless the STRING type become a real object, that may not be simple to support, making it an object isn't a simple task, due the quirks that are handled by the compiler internally, unfortunately.

If it was a class, the users could choose between left( foo, 1 ) and foo.left( 1 ) etc.


well, adding a new (internal) quirk isn't that bad, especially if it's for a good cause ;)

introducing a new constructor/destructor for strings wouldn't be a problem IMO, it doesn't have to become a full object, only allow new/delete to work normally with strings (just like other datatypes), fair deal i guess. :)

Return to “DOS”

Who is online

Users browsing this forum: No registered users and 3 guests