Folowing with this: I've learned how to use gdb and I think I've found where the error is.
There is the code line:
1 | ... |
2 | /* In the header file */ |
3 | typedef double BAS_NUMBER; |
4 | ... |
5 | |
6 | /* Somewhere in the initialization */ |
7 | basDataStackSize = 512; |
8 | ... |
9 | |
10 | /* In the function */ |
11 | struct _PILA_DATOS_ *Resultado = NULL; |
12 | |
13 | if ((Resultado = (struct _PILA_DATOS_*)malloc(sizeof(struct _PILA_DATOS_))) |
14 | != NULL) |
15 | { |
16 | Resultado->Dato = NULL; Resultado->Tipo = NULL; |
17 | if ((Resultado->Dato = (BAS_NUMBER*)malloc ( |
18 | sizeof(BAS_NUMBER) * basDataStackSize)) != NULL) |
19 | { |
20 | ... |
And this is what gdb says:
1 | 39 struct _PILA_DATOS_ *Resultado = NULL; |
2 | (gdb) n |
3 | 41 if ((Resultado = (struct _PILA_DATOS_*)malloc(sizeof(struct _PILA_DATOS_))) |
4 | (gdb) |
5 | 44 Resultado->Dato = NULL; Resultado->Tipo = NULL; |
6 | (gdb) p basDataStackSize |
7 | $15 = 512 |
8 | (gdb) n |
9 | 45 if ((Resultado->Dato = (BAS_NUMBER*)malloc ( |
10 | (gdb) n |
11 | warning: HEAP[basrun.exe]: |
12 | warning: HEAP: Free Heap block 3d57f8 modified at 3d580c after it was freed |
13 | |
14 | |
15 | Program received signal SIGTRAP, Trace/breakpoint trap. |
16 | 0x7c911231 in ntdll!DbgUiConnectToDbg () from ntdll.dll |
17 | (gdb) p Resultado->Dato |
18 | No symbol "Resultado" in current context. |
19 | (gdb) n |
20 | Single stepping until exit from function ntdll!DbgUiConnectToDbg, |
21 | which has no line number information. |
22 | 0x7c97c943 in ntdll!RtlpNtMakeTemporaryKey () from ntdll.dll |
23 | (gdb) n |
24 | Single stepping until exit from function ntdll!RtlpNtMakeTemporaryKey, |
25 | which has no line number information. |
26 | 0x7c959a2e in ntdll!RtlInsertElementGenericTableAvl () from ntdll.dll |
27 | (gdb) n |
28 | Single stepping until exit from function ntdll!RtlInsertElementGenericTableAvl, |
29 | |
30 | which has no line number information. |
31 | 0x7c921e8f in ntdll!RtlInitializeHandleTable () from ntdll.dll |
32 | (gdb) n |
33 | Single stepping until exit from function ntdll!RtlInitializeHandleTable, |
34 | which has no line number information. |
35 | 0x7c920c31 in wcsncpy () from ntdll.dll |
36 | (gdb) n |
37 | Single stepping until exit from function wcsncpy, |
38 | which has no line number information. |
39 | 0x7c928251 in ntdll!RtlRealPredecessor () from ntdll.dll |
40 | (gdb) n |
41 | Single stepping until exit from function ntdll!RtlRealPredecessor, |
42 | which has no line number information. |
43 | 0x7c921b54 in ntdll!RtlInitializeHandleTable () from ntdll.dll |
44 | (gdb) n |
45 | Single stepping until exit from function ntdll!RtlInitializeHandleTable, |
46 | which has no line number information. |
47 | 0x7c94c087 in ntdll!RtlInsertElementGenericTableAvl () from ntdll.dll |
48 | (gdb) n |
49 | Single stepping until exit from function ntdll!RtlInsertElementGenericTableAvl, |
50 | |
51 | which has no line number information. |
52 | 0x7c9130f5 in ntdll!RtlFindCharInUnicodeString () from ntdll.dll |
53 | (gdb) n |
54 | Single stepping until exit from function ntdll!RtlFindCharInUnicodeString, |
55 | which has no line number information. |
56 | 0x7c94c0a0 in ntdll!RtlInsertElementGenericTableAvl () from ntdll.dll |
57 | (gdb) n |
58 | Single stepping until exit from function ntdll!RtlInsertElementGenericTableAvl, |
59 | |
60 | which has no line number information. |
61 | 0x7c921bba in ntdll!RtlInitializeHandleTable () from ntdll.dll |
62 | (gdb) n |
63 | Single stepping until exit from function ntdll!RtlInitializeHandleTable, |
64 | which has no line number information. |
65 | 0x7c920c86 in wcsncpy () from ntdll.dll |
66 | (gdb) n |
67 | Single stepping until exit from function wcsncpy, |
68 | which has no line number information. |
69 | 0x7c921bdc in ntdll!RtlInitializeHandleTable () from ntdll.dll |
70 | (gdb) n |
71 | Single stepping until exit from function ntdll!RtlInitializeHandleTable, |
72 | which has no line number information. |
73 | 0x7c920bcc in wcsncpy () from ntdll.dll |
74 | (gdb) n |
75 | Single stepping until exit from function wcsncpy, |
76 | which has no line number information. |
77 | 0x7c921bff in ntdll!RtlInitializeHandleTable () from ntdll.dll |
78 | (gdb) c <<< I was boring so I decided to continue. |
79 | Continuing. |
80 | |
81 | Uso: |
82 | basrun <archivo.prg> |
83 | |
84 | warning: HEAP[basrun.exe]: |
85 | warning: Heap block at 003D24A8 modified at 003D2539 past requested size of 89 |
86 | |
87 | |
88 | Program received signal SIGTRAP, Trace/breakpoint trap. |
89 | 0x7c911231 in ntdll!DbgUiConnectToDbg () from ntdll.dll |
90 | (gdb) c |
91 | Continuing. |
92 | warning: HEAP[basrun.exe]: |
93 | warning: Invalid Address specified to RtlFreeHeap( 003D0000, 003D24B0 ) |
94 | |
95 | |
96 | Program received signal SIGTRAP, Trace/breakpoint trap. |
97 | 0x7c911231 in ntdll!DbgUiConnectToDbg () from ntdll.dll |
98 | (gdb) c |
99 | Continuing. |
100 | warning: HEAP[basrun.exe]: |
101 | warning: Heap block at 003D52E0 modified at 003D57E8 past requested size of 500 |
102 | |
103 | |
104 | Program received signal SIGTRAP, Trace/breakpoint trap. |
105 | 0x7c911231 in ntdll!DbgUiConnectToDbg () from ntdll.dll |
106 | (gdb) c |
107 | Continuing. |
108 | warning: HEAP[basrun.exe]: |
109 | warning: Invalid Address specified to RtlFreeHeap( 003D0000, 003D52E8 ) |
110 | |
111 | |
112 | Program received signal SIGTRAP, Trace/breakpoint trap. |
113 | 0x7c911231 in ntdll!DbgUiConnectToDbg () from ntdll.dll |
114 | (gdb) c |
115 | Continuing. |
116 | |
117 | Program exited with code 01. |
118 | (gdb) |
What's happening?
I think this is a delayed error. I think you free something and then modify it later, and the system catches it next time it does a malloc. In conclusion, make sure you set variables to NULL after you delete them, and try it out.
I don't think so. Anyway I've checked all "free" calls I do in all modules and I set all variables to NULL after delete them and also before create them (malloc) and I have same the error.
Unluckily CGP is right. You have a buffer overrun somewhere in your code. Your best chance is beginning to comment code until you have your program running, and then adding code until it breaks again.
Find it. Oh, man! That was the most stupid error I've done ever: I just put "Cnt<=basNumLabels" instead of "Cnt<basNumLabels" in a for loop . Now it runs.
But I can't understand why it fails in Windows but run perfect in GNU/Linux.
Anyway, thanks: you make me think and check each bit of the program. I thought I never find it. Can't send cookies, sorry but I'll thank you in the "readme" file of the final release
The reason is that MSVCRT's debugging library does heap checking, and GNU/Linux's glibc doesn't (well, maybe the debugging version does, but I doubt you were using it). Valgrind would have found this error, though.
It is not on by default, there must be a switch to turn it on.