I need to read a complicated data structure from files. I run into strange issue while tried to implement it.
This code causes memory corruption ( BGDC 1.0.0 (Jun 20 2010 23:59:57) (r147), Linux ):
[code language="bennu"]import "mod_video"
import "mod_map"
import "mod_wm"
import "mod_grproc"
import "mod_mouse"
import "mod_key"
import "mod_file"
import "mod_dir"
import "mod_mem"
import "mod_say"
import "mod_string"
import "mod_rand"
/*-------------------------------------------*/
const
MAX_FF_FILES = 512;
MAX_FF_FRAMES = 512;
MAX_FF_ANIS = 512;
MAX_FF_IMGS = 512;
MAX_IMG_FRAMES = 64;
end
type FILEREC
int ID;
string fpath;
int typ;
end;
type FRAMEREC
int ID;
int A, S;
string Name;
int map = -1;
FILEREC *ref;
end;
type IMAGEFRAME
int x1, y1;
int x2, y2;
int w, h;
end;
type IMAGEREC
int ID;
string name;
int map = -1;
FILEREC *ref;
int w, h;
IMAGEFRAME ifram[ MAX_IMG_FRAMES ];
int icnt;
end;
type ResIndex
FILEREC files[ MAX_FF_FILES ];
int count;
FRAMEREC frames[ MAX_FF_FRAMES ];
IMAGEREC images[ MAX_FF_IMGS ];
int fcount;
int icount;
end;
/*-------------------------------------------*/
function load_data( ResIndex *data, string fld )
local
int fd, ofs;
int i, j, k, t;
string name;
end
begin
data.count = 0;
//fd = fopen( fld+"/INDEX", O_READ );
fd = 300;
if( fd )
while( /*!feof(fd)*/ fd-->0 && data.count<MAX_FF_FILES )
//name = fgets( fd );
name = rand(1,100);
data.files[data.count].ID = atoi( name );
//name = fgets( fd );
name = "file."+rand(1,100);
data.files[data.count].fpath = fld +'/'+name;
say('FILE['+data.count+'] '+data.files[data.count].ID+' / '+data.files[data.count].fpath);
data.count++;
end
//fclose( fd );
else
say( "Can't open INDEX" );
return;
end
end
/*-------------------------------------------*/
#define waitk( k ) while( !key(_ESC) ) frame; end; while( key(_ESC) ) frame; end
Main()
local
ResIndex interface;
end
begin
load_data( &interface, "." );
waitk( _ESC );
end[/code]
Output:
FILE[0] 84 / ./file.87
FILE[1] 78 / ./file.16
FILE[2] 94 / ./file.36
FILE[3] 87 / ./file.93
FILE[4] 50 / ./file.22
FILE[5] 63 / ./file.28
FILE[6] 91 / ./file.60
FILE[7] 64 / ./file.27
FILE[8] 41 / ./file.27
FILE[9] 73 / ./file.37
FILE[10] 12 / ./file.69
FILE[11] 68 / ./file.30
FILE[12] 83 / ./file.31
FILE[13] 63 / ./file.24
FILE[14] 68 / ./file.36
FILE[15] 30 / ./file.3
FILE[16] 23 / ./file.59
FILE[17] 70 / ./file.68
FILE[18] 94 / ./file.57
FILE[19] 12 / ./file.43
FILE[20] 30 / ./file.74
FILE[21] 22 / ./file.20
FILE[22] 85 / ./file.38
FILE[23] 99 / ./file.25
FILE[24] 16 / ./file.71
FILE[25] 14 / ./file.27
FILE[26] 92 / ./file.81
FILE[27] 57 / ./file.74
FILE[28] 63 / ./file.71
FILE[29] 97 / ./file.82
FILE[30] 6 / ./file.26
FILE[31] 85 / ./file.28
FILE[32] 37 / ./file.6
FILE[33] 47 / ./file.30
FILE[34] 14 / ./file.58
FILE[35] 25 / ./file.96
FILE[36] 83 / ./file.46
FILE[37] 15 / ./file.68
FILE[38] 35 / ./file.65
FILE[39] 44 / ./file.51
FILE[40] 88 / ./file.9
FILE[41] 77 / ./file.79
FILE[42] 89 / ./file.85
FILE[43] 4 / ./file.52
FILE[44] 55 / ./file.100
FILE[45] 33 / ./file.61
FILE[46] 77 / ./file.69
FILE[47] 40 / ./file.13
FILE[48] 27 / ./file.87
FILE[49] 95 / ./file.40
FILE[50] 96 / ./file.71
FILE[51] 35 / ./file.79
FILE[52] 68 / ./file.2
FILE[53] 98 / ./file.3
FILE[54] 18 / ./file.93
FILE[55] 53 / ./file.57
FILE[56] 2 / ./file.81
FILE[57] 87 / ./file.42
FILE[58] 66 / ./file.90
FILE[59] 45 / ./file.20
FILE[60] 41 / ./file.30
FILE[61] 32 / ./file.18
FILE[62] 98 / ./file.72
FILE[63] 82 / ./file.76
FILE[64] 10 / ./file.28
FILE[65] 68 / ./file.57
FILE[66] 98 / ./file.54
FILE[67] 87 / ./file.66
FILE[68] 7 / ./file.84
FILE[69] 20 / ./file.25
FILE[70] 29 / ./file.72
FILE[71] 33 / ./file.30
FILE[72] 4 / ./file.20
FILE[73] 71 / ./file.69
FILE[74] 9 / ./file.16
FILE[75] 41 / ./file.50
FILE[76] 97 / ./file.24
FILE[77] 19 / ./file.46
FILE[78] 47 / ./file.52
FILE[79] 22 / ./file.56
FILE[80] 80 / ./file.89
FILE[81] 65 / ./file.29
*** glibc detected *** main.dcb: double free or corruption (!prev): 0x08957008 ***
======= Backtrace: =========
/lib/libc.so.6[0xb773cdb1]
/lib/libc.so.6[0xb773e4da]
/lib/libc.so.6[0xb7742151]
/lib/libc.so.6(realloc+0xdd)[0xb77425cd]
../bgd-runtime/libbgdrtm.so[0xb76a8b3e]
../bgd-runtime/libbgdrtm.so[0xb76a8d45]
../bgd-runtime/libbgdrtm.so(string_add+0xa3)[0xb76a9513]
../bgd-runtime/libbgdrtm.so(instance_go+0x848)[0xb76ac558]
../bgd-runtime/libbgdrtm.so(instance_go+0x2462)[0xb76ae172]
../bgd-runtime/libbgdrtm.so(instance_go_all+0xed)[0xb76ae77d]
main.dcb[0x8048d95]
/lib/libc.so.6(__libc_start_main+0xe6)[0xb76e7a66]
main.dcb[0x8048af1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:07 931289 /home/eugene/Wiz/bennu/bugs/bgd-runtime/bgdi
0804a000-0804b000 r--p 00001000 08:07 931289 /home/eugene/Wiz/bennu/bugs/bgd-runtime/bgdi
0804b000-0804c000 rw-p 00002000 08:07 931289 /home/eugene/Wiz/bennu/bugs/bgd-runtime/bgdi
08957000-08b9b000 rw-p 00000000 00:00 0 [heap]
b6300000-b6321000 rw-p 00000000 00:00 0
b6321000-b6400000 ---p 00000000 00:00 0
b6498000-b6564000 rw-p 00000000 00:00 0
b6564000-b6565000 ---p 00000000 00:00 0
b6565000-b6d65000 rw-p 00000000 00:00 0
b6d65000-b6d9f000 rw-p 00000000 00:00 0
b6dc1000-b6e0c000 rw-s 00000000 00:04 5734410 /SYSV00000000 (deleted)
b6e0c000-b6e14000 r-xp 00000000 08:05 42471 /usr/lib/libXcursor.so.1.0.2
b6e14000-b6e15000 r--p 00008000 08:05 42471 /usr/lib/libXcursor.so.1.0.2
b6e15000-b6e16000 rw-p 00009000 08:05 42471 /usr/lib/libXcursor.so.1.0.2
b6e44000-b6f32000 r--p 001bc000 08:05 186798 /usr/lib/locale/locale-archive
b6f32000-b7132000 r--p 00000000 08:05 186798 /usr/lib/locale/locale-archive
b7132000-b7139000 r-xp 00000000 08:05 42553 /usr/lib/libXrandr.so.2.2.0
b7139000-b713a000 r--p 00006000 08:05 42553 /usr/lib/libXrandr.so.2.2.0
b713a000-b713b000 rw-p 00007000 08:05 42553 /usr/lib/libXrandr.so.2.2.0
b713b000-b7143000 r-xp 00000000 08:05 20943 /usr/lib/libXrender.so.1.3.0
b7143000-b7144000 r--p 00007000 08:05 20943 /usr/lib/libXrender.so.1.3.0
b7144000-b7145000 rw-p 00008000 08:05 20943 /usr/lib/libXrender.so.1.3.0
b7145000-b7153000 r-xp 00000000 08:05 42491 /usr/lib/libXext.so.6.4.0
b7153000-b7154000 r--p 0000d000 08:05 42491 /usr/lib/libXext.so.6.4.0
b7154000-b7155000 rw-p 0000e000 08:05 42491 /usr/lib/libXext.so.6.4.0
b7157000-b7174000 r-xp 00000000 08:05 124799 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/libgcc_s.so.1
b7174000-b7175000 r--p 0001c000 08:05 124799 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/libgcc_s.so.1
b7175000-b7176000 rw-p 0001d000 08:05 124799 /usr/lib/gcc/i686-pc-linux-gnu/4.4.2/libgcc_s.so.1
b7176000-b717d000 r--s 00000000 08:05 52105 /usr/lib/gconv/gconv-modules.cache
b717d000-b7181000 r-xp 00000000 08:05 42497 /usr/lib/libXfixes.so.3.1.0
b7181000-b7182000 r--p 00003000 08:05 42497 /usr/lib/libXfixes.so.3.1.0
b7182000-b7183000 rw-p 00004000 08:05 42497 /usr/lib/libXfixes.so.3.1.0
b7183000-b7184000 r-xp 00000000 08:07 932078 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_rand.so
b7184000-b7185000 r--p 00000000 08:07 932078 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_rand.so
b7185000-b7186000 rw-p 00001000 08:07 932078 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_rand.so
b7186000-b71a9000 r-xp 00000000 08:05 37620 /usr/lib/libpng12.so.0
b71a9000-b71aa000 r--p 00022000 08:05 37620 /usr/lib/libpng12.so.0
b71aa000-b71ab000 rw-p 00023000 08:05 37620 /usr/lib/libpng12.so.0
b71ac000-b71ae000 r-xp 00000000 08:07 1797043 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_string.so
b71ae000-b71af000 r--p 00001000 08:07 1797043 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_string.so
b71af000-b71b0000 rw-p 00002000 08:07 1797043 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_string.so
b71b0000-b71b1000 r-xp 00000000 08:07 931292 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_say.so
b71b1000-b71b2000 r--p 00000000 08:07 931292 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_say.so
b71b2000-b71b3000 rw-p 00001000 08:07 931292 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_say.so
b71b3000-b71b4000 r-xp 00000000 08:07 1797063 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_mem.so
b71b4000-b71b5000 r--p 00001000 08:07 1797063 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_mem.so
b71b5000-b71b6000 rw-p 00002000 08:07 1797063 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_mem.so
b71b6000-b71b8000 r-xp 00000000 08:07 1797030 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_dir.so
b71b8000-b71b9000 r--p 00001000 08:07 1797030 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_dir.so
b71b9000-b71ba000 rw-p 00002000 08:07 1797030 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_dir.so
b71ba000-b71bc000 r-xp 00000000 08:07 1771140 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_file.so
b71bc000-b71bd000 r--p 00001000 08:07 1771140 /home/eugene/Wiz/bennu/bugs/bgd-runtime/mod_file.so./run.sh
If I change number of string concatenations ( commenting say() ) program runs longer before fail;
It is possible that bug is on my side, but I can't find it :(
:o Can't help you... If the structures are ok (Like it seems) you are not doing rare things...
Have you tried it on Windows or with another version?
string data type, is a data type very special, I was never like it.
don't use pointers to strings or pointers to structs with strings in his members.
try with this structs:
type FILEREC
int ID;
char fpath[256];
int typ;
end;
type FRAMEREC
int ID;
int A, S;
char Name[256];
int map = -1;
FILEREC *ref;
end;
type IMAGEFRAME
int x1, y1;
int x2, y2;
int w, h;
end;
type IMAGEREC
int ID;
char name[256];
int map = -1;
FILEREC *ref;
int w, h;
IMAGEFRAME ifram[ MAX_IMG_FRAMES ];
int icnt;
end;