The ATL CTime implementation uses inline functions and static data.
> > There seems to be a bug in de VS .net C++ compiler (optimization) when
> using
[quoted text clipped - 29 lines]
>
> Jeff F
> Out of curiosity, where's the inline function, and where's the static data?
>
> Jeff F
I used the CTime class because that class caused te trouble in my
application ( ATL CTime uses inline functions and static data ).
But maybe the following code demonstrates more directly the effect:
#include "stdafx.h"
static int iStatic;
class CBuggy
{
public:
CBuggy(int iValue);
inline int GetValue();
private:
int _iValue;
};
CBuggy::CBuggy(int iValue) : _iValue(iValue) { }
int CBuggy::GetValue()
{
return (iStatic = _iValue);
}
int _tmain(int argc, _TCHAR* argv[])
{
CBuggy b1(1);
CBuggy b2(2);
printf("b1 = %d, b2 = %d\n", b1.GetValue(), b2.GetValue());
return 0;
}
---
Output is:
b1 = 1, b2 = 1 (but one would expect that b2 = 2).
When the GetValue() function is not inline all goes well.
The only thing that helps is disabling the inline function. The compiler
generates wrong code (even without optimization it seems ).
Assembly output when the function is inline:
mov eax, DWORD PTR _b2$[ebp]
mov DWORD PTR _iStatic, eax
mov ecx, DWORD PTR _b1$[ebp]
mov DWORD PTR _iStatic, ecx
mov edx, DWORD PTR _iStatic
push edx
mov eax, DWORD PTR _iStatic
push eax
push OFFSET FLAT:$SG71576
call _printf
After the instruction that moves eax to address _iStatic, the value of eax
should have been pushed on the stack. But the compiler delays this
instruction until after the second inline function. And than it's too late
because the value of iStatic has already changed again.
Carl Daniel [VC++ MVP] - 03 Dec 2004 16:19 GMT
>> Out of curiosity, where's the inline function, and where's the
>> static data?
[quoted text clipped - 4 lines]
> application ( ATL CTime uses inline functions and static data ).
> But maybe the following code demonstrates more directly the effect:
Simple repros that don't depend on any libraries are always best.
This bug is fixed in the VC++ 2005 beta.
-cd
Ayman Shoukry [MSFT] - 27 Dec 2004 23:11 GMT
I verified that the issue is actually fixed in VC 2005. FYI, I have also
logged this as issue# 583186 for future references.
--------------------
| Thread-Topic: Inline function that return static data bug
| thread-index: AcTZUdo9ajJL33QDTcGYcRwpqfNkVQ==
| X-WBNR-Posting-Host: 195.109.94.235
| From: "=?Utf-8?B?QmVydCBKYW5zZW4=?="
<BertJansen@discussions.microsoft.com>
| References: <8810C575-27A6-4525-AD39-8BD17ADACD89@microsoft.com>
<u8vVcdU2EHA.2016@TK2MSFTNGP15.phx.gbl>
| Subject: Re: Inline function that return static data bug
| Date: Fri, 3 Dec 2004 08:05:05 -0800
[quoted text clipped - 12 lines]
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
| Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
| Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.vc:42868
| X-Tomcat-NG: microsoft.public.dotnet.languages.vc
[quoted text clipped - 60 lines]
| instruction until after the second inline function. And than it's too late
| because the value of iStatic has already changed again.

Signature
Ayman Shoukry, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.