Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2004

Tip: Looking for answers? Try searching our database.

Inline function that return static data bug

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bert Jansen - 03 Dec 2004 14:31 GMT
There seems to be a bug in de VS .net C++ compiler (optimization) when using
inline functions that return static data.

The following code demonstrates this (Win32 console app with ATL support):

#include "stdafx.h"
#include <atltime.h>

int _tmain(int argc, _TCHAR* argv[])
{
  CTime nov(2004, 11, 1, 0, 0, 0, 0);
  CTime dec(2004, 12, 1, 0, 0, 0 ,0);

  printf("nov_month = %d, dec_month = %d\n",
            nov.GetMonth(),  dec.GetMonth());

  return 0;
}

When compiled without optimization the output is:
nov_month = 11, dec_month = 12

But when compiled with optimization the output is:
nov_month = 11, dec_month = 11

Msdn only reports that a comparable bug existed in Visual Studio 6 en not in
Visual Studio .Net but thats not correct I think.
Jeff F - 03 Dec 2004 14:49 GMT
> There seems to be a bug in de VS .net C++ compiler (optimization) when using
> inline functions that return static data.
[quoted text clipped - 23 lines]
> Msdn only reports that a comparable bug existed in Visual Studio 6 en not in
> Visual Studio .Net but thats not correct I think.

Out of curiosity, where's the inline function, and where's the static data?

Jeff F
Bert Jansen - 03 Dec 2004 15:13 GMT
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
Bert Jansen - 03 Dec 2004 16:05 GMT
> 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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.