Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class constructor disassembly

There is a small section of disassembly after the call to the constructor, that does not make any sense. Here it is,

.text:011A18F0 loc_11A18F0:                            ; CODE XREF: main+5Bj
.text:011A18F0                 mov     [ebp+again_obj], 0
.text:011A18FA
.text:011A18FA loc_11A18FA:                            ; CODE XREF: main+6Ej
.text:011A18FA                 mov     eax, [ebp+again_obj]
.text:011A1900                 mov     [ebp+var_104], eax
.text:011A1906                 mov     [ebp+var_4], 0FFFFFFFFh
.text:011A190D                 mov     ecx, [ebp+var_104]
.text:011A1913                 mov     [ebp+var_14], ecx
.text:011A1916                 mov     eax, [ebp+var_14]
.text:011A1919                 mov     [ebp+var_E0], eax
.text:011A191F                 mov     ecx, [ebp+var_E0]
.text:011A1925                 mov     [ebp+var_EC], ecx
.text:011A192B                 cmp     [ebp+var_EC], 0
.text:011A1932                 jz      short loc_11A1949

I don't understand why 0FFFFFFFFh is moved into var_4, and why a lot of values are moved in and out of the variables, the way it is done here. The optimization has been turned of and I compiled the source code on a VS2010 platform.


1 Answers

var_4 is an internal variable (I'll call it '_state') which tracks the state of constructed objects for unwinding in case the exception happens. What you see is a common pattern made by VC++ when dealing with new'ed objects. E.g.

 A* pA1 = new A();

is converted to something like this:

_state = -1;
...
A *temp_pA1 = operator new(sizeof(A));
_state = 0;
if ( temp_pA1 != NULL ) 
{
   pA1 = A::A(temp_pA1);
}
else
{
   pA1 = NULL;
}
_state = -1;
...
[unwind funclet for state == 0]
{
  A::~A(temp_pA1);
}

As you see, it's set to some value (e.g. 0) after a block of memory for the object is new'ed but the object is not constructed yet. This way, if an exception happens in the constructor, the exception handler will free the allocated memory automatically. After the construction it's set to -1, which roughly corresponds to "the automatic stuff is done, now all memory management is in the programmer's hands".

See here for more details ("C++ Exception Model Implementation" and "Sample Program with C++ Exceptions").

like image 121
Igor Skochinsky Avatar answered Sep 16 '26 14:09

Igor Skochinsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!