I have a problem with compiling some code for mingw32, linking a mingw32-compiled library. When I call a member function (i.e. constructor) of a class in the library that I am dynamically linking to, the arguments are pushed onto the stack in the wrong order.
Here's an example:
Program received signal SIGSEGV, Segmentation fault.
0x65e717e9 in TCODMap::TCODMap (this=0x40, width=48, height=40)
at src/fov.cpp:28
28 src/fov.cpp: No such file or directory.
(gdb) backtrace
#0 0x65e717e9 in TCODMap::TCODMap (this=0x40, width=48, height=40)
at src/fov.cpp:28
#1 0x00401bda in map::map (this=0x8683c8, dim=..., fill=0 '\000')
at src/map.cpp:12
#2 0x004017d7 in next_init () at src/main.cpp:24
#3 0x00401b8e in main (argc=1, argv=0x862fc8) at src/main.cpp:98
My call to the TCODMap constructor has the arguments width=64
and height=48
, however notably the this
implicit argument is set to 64 (0x40), the width
is set to 48, and the height
is set to some garbage value 40. In this case, the constructor is called as part of an initializer for another class that I have in my user code.
The code in question:
map::map(loc dim, uint8 fill) : _dim(dim), tcodmap(_dim.x, _dim.y), actors()
{
_size = _dim.x * _dim.y;
_data = new uint8[_size];
for (int xi = 0; xi < _dim.x; xi++)
{
for (int yi = 0; yi < _dim.y; yi++)
{
put(xi, yi, fill);
}
}
}
It seems the thiscall
calling convention, which says the this
pointer should be pushed last onto the stack such that it is lexically first in the argument list, is not being obeyed correctly.
How do I fix this?
I wrote a test program to check what you trying to do. 1st of all you are getting the error at run time and not at compile time as you have provided a stack trace of gdb.
Stacktrace:
Point here is that the 'this' implicit pointer would point to a valid address until unless your program is causing "stack buffer overflow" which corrupts the memory assigned to 'this' pointer for tcodmap.
You should share more details of complete code, how you use this program. Do you experince this issue at every run or is it random?