Visual C++ Beta 2

G

Guest

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:blush:ldSyntax 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->GetValue(0));
//exception here
if ((fieldvalue & iAttribValue) == fieldvalue)
{
Console::WriteLine(fields->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?
 
J

Jochen Kalmbach [MVP]

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:blush:ldSyntax 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>

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->GetValue(0));
//exception here
if ((fieldvalue & iAttribValue) == fieldvalue)
{
Console::WriteLine(fields->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?


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.


--
Greetings
Jochen

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

Jochen Kalmbach [MVP]

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"...


--
Greetings
Jochen

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top