Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-02

FPC Problem with reAllocMem...

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Mills

Hi all,

I've annoyed you guys enough with pointers, objects and clases :P I'm sorry :)

This bugs me though as to why this program crashes at the 4th call to
addNick(nick: String);

Here is the gdb results, and attached is the source code if anyone is willing to have a look.

thank you very much :)

James

$ gdb datatest
gdb: Symbol `emacs_ctlx_keymap' has different size in shared object, consider re-linking GNU gdb 2002-04-01-cvs
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-linux"...
(gdb) break datanicks.pas:47
Breakpoint 1 at $8058f2d: file datanicks.pas, line 47. (gdb) run
Starting program: /home/pircsrv/src/test/datanicks/datatest sizeOf(TNick) = 4
Memory available before getMem: 261856
Memory available after getMem: 261840
Command: a
Nick to add: prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)

Breakpoint 1, ADDNICK (valDATA='prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)') at datanicks.pas:47
47 MM := memAvail;
(gdb) c
Continuing.
Memory available before addNick: 261792
string = prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com) num tokens = 9
ll is well in FPC
Memory available after addNick: 259680
Command: a
Nick to add: prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)

Breakpoint 1, ADDNICK (valDATA='prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)') at datanicks.pas:47
47 MM := memAvail;
(gdb) c
Continuing.
Memory available before addNick: 259712
string = prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com) num tokens = 9
All is well in FPC
Memory available after addNick: 257600
Command: a
Nick to add: prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)

Breakpoint 1, ADDNICK (valDATA='prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)') at datanicks.pas:47
47 MM := memAvail;
(gdb) c
Continuing.
Memory available before addNick: 257632
string = prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com) num tokens = 9
All is well in FPC
Memory available after addNick: 255520
Command: a
Nick to add: prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)

Breakpoint 1, ADDNICK (valDATA='prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)') at datanicks.pas:47
47 MM := memAvail;
(gdb) c
Continuing.
Memory available before addNick: 255552
string = prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com) num tokens = 9

Program received signal SIGSEGV, Segmentation fault. $08062697 in _SYSLINUX ()
(gdb) bt
#0 $08062697 in _SYSLINUX ()
#1 $080628ab in _SYSLINUX ()
#2 $08061f23 in _SYSLINUX ()
#3 $080591c6 in ADDNICK (valDATA='prologic 1 1044896365 prologic daisy test.prologitech.com 0 :ProLogiTech (www.prologitech.com)') at datanicks.pas:66
#4 $08065b17 in main () at datatest.pas:21
(gdb)

James Mills
to James Mills

Another interesting thing to note as well which I think is rather strange...

if you change in datanicks.pas lien 65,
reAllocMem(nicks, sizeOf(TNick) * nNicks);
to
getMem(nicks, sizeOf(TNick) * nNicks);

It will crash the same way as before, but at the 5th call of addNick

cheers
James

Anton Tichawa
to James Mills: I've annoyed you guys enough with pointers, objects and clases :P I'm sorry :) This bugs…

Hello, James!

You have to allocate memory BEFORE storing data there, i. e. you should change datanick.pas lines about 63 .. 66 from:

  nicks[nNicks] := TNick.init(nick, hops, signon, ident, host,
    server, unused, name);
  {getMem(nicks, sizeOf(TNick) * nNicks);}
  inc(nNicks);
  reAllocMem(nicks, sizeOf(TNick) * nNicks);

to

  {getMem(nicks, sizeOf(TNick) * nNicks);}
  inc(nNicks);
  reAllocMem(nicks, sizeOf(TNick) * nNicks);
  nicks[nNicks - 1] := TNick.init(nick, hops, signon, ident, host,
    server, unused, name);

Note that the storage index changed to nNicks - 1.

HTH

Anton Tichawa.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------