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++ / May 2005

Tip: Looking for answers? Try searching our database.

Visual C++ Beta 2

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PaulW@community.nospam - 10 May 2005 12:38 GMT
When I compile the following code with the C++ compiler version
14.00.50215.44 (Microsoft Visual Studio 2005 beta 2) using the command line
'cl -W4 -clr:oldSyntax test.cpp' at run time I get a Null Reference Exception
due to the dynamic_cast. With the C++ compiler version 13.10.3077 from Visual
Studio 2003 using the command line 'cl -W4 -clr test.cpp' no such error is
generated.

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

public __gc class AttributesSample
{
public:
   void Mymethod (int, [Out] String** str2m, String**)
   {
       *str2m = S"in Mymethod";
   }
};

void PrintAttributes(Type* attribType, int iAttribValue)
{
   if (attribType->IsEnum)
   {
       FieldInfo* fields[] =
attribType->GetFields(static_cast<BindingFlags>(BindingFlags::Public |
BindingFlags::Static));
        for (int i = 0; i < fields->Length; i++)
        {
            int fieldvalue = *dynamic_cast<Int32*>(fields[i]->GetValue(0));
//exception here
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console::WriteLine(fields[i]->Name);
            }
        }
    }
}

int main()
{      
   Type* MyType = Type::GetType(S"AttributesSample");
   MethodBase* Mymethodbase = MyType->GetMethod(S"Mymethod");
   MethodAttributes Myattributes = Mymethodbase->Attributes;
   PrintAttributes(__typeof(System::Reflection::MethodAttributes), (int)
Myattributes);
   return 0;
}

Is this a bug?
Jochen Kalmbach [MVP] - 10 May 2005 13:02 GMT
Hi Paul!
> When I compile the following code with the C++ compiler version
> 14.00.50215.44 (Microsoft Visual Studio 2005 beta 2) using the command line
> 'cl -W4 -clr:oldSyntax test.cpp' at run time I get a Null Reference Exception
> due to the dynamic_cast. With the C++ compiler version 13.10.3077 from Visual
> Studio 2003 using the command line 'cl -W4 -clr test.cpp' no such error is
> generated.

It does not matter if you use the command-line or the IDE.

> #using <mscorlib.dll>
>
[quoted text clipped - 41 lines]
>
> Is this a bug?

It seems to be!

The problem is that the VC2003 compiler generated the following IL-code:

<snipped>
obj1 = infoArray1[num1].GetValue(null);
int num1 = (int) obj1;
</snipped>

And the VC2005 B2 compiler /with /oldsyntax) generates:

<snipped>
obj1 = infoArray1[num1].GetValue(null);
int num1 = *(!(obj1 is int) ? 0 : ((int) obj1));
</snipped>

So please report a (detailed!) bug-report at:
http://lab.msdn.microsoft.com/productfeedback/

If you do not want to do this, please let me know, than I can do it for you.

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

Jochen Kalmbach [MVP] - 10 May 2005 13:07 GMT
>> Is this a bug?
>
> It seems to be!

But it looks like a bug in the VC2003 compiler!

Because "dynamic_cast" should excatly do what the VC2005 compiler is doing:
Check if the object has the correct type (or derived) and then cast it
to this type or return "null".

> The problem is that the VC2003 compiler generated the following IL-code:
>
> <snipped>
> obj1 = infoArray1[num1].GetValue(null);
> int num1 = (int) obj1;
> </snipped>

This is wrong...

> And the VC2005 B2 compiler /with /oldsyntax) generates:
>
> <snipped>
> obj1 = infoArray1[num1].GetValue(null);
> int num1 = *(!(obj1 is int) ? 0 : ((int) obj1));
> </snipped>

This is correct.

But it should be documented in the "breaking-changes"...

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

PaulW@community.nospam - 10 May 2005 13:44 GMT
Hi Jochen,

Thanks for the reply. Although I found the problem first in my own code I
based it on the sample code (and reproduced it in the sample code as well).
The sample code is at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fSystemTypeClassGetFieldsTopic2.asp
.

Therefore it looks like this sample code is wrong.

I have submitted a bug report anyway.

Regards,
Paul.
Jochen Kalmbach [MVP] - 10 May 2005 13:49 GMT
Hi Paul!

> I have submitted a bug report anyway.

Can you please post the link, so other people can easily find it...

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

PaulW@community.nospam - 10 May 2005 14:06 GMT
> Can you please post the link, so other people can easily find it...

The link is:

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=fa4a6
137-dac8-48a7-9413-a6abd826a899


Regards,
Paul

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.