Compiler bug in VC++ .Net 2003

Hi

I have very strange situations.
The following code always crashes any .Net appication that uses it.

What I have:
1. The Managed C++ code (test.h and test.cpp) that defines enumeration "Els" and function that returns an array of values of this enum "Els" as output parameter "outParam".

-------------------------------- test.h --------------------------
#pragma once

using namespace System::Runtime::InteropServices;

#define V_OK    17235968
#define V_HALT  17235969
#define V_CRASH 17235970

namespace managedC

    public __value enum Els : unsigned int
    {
        Ok    = V_OK,
        Halt  = V_HALT,
        Crash = V_CRASH
    };
 public __gc class testClass
 {
  public:
   static void TestMethodEnum([OutAttribute] Els (*outParam)[]);
 };
}
-------------------------------- test.h --------------------------
-------------------------------- test.cpp --------------------------
#include "test.h"

#using <mscorlib.dll>

namespace managedC
{
    void testClass::TestMethodEnum([OutAttribute] Els (*outParam)[])
    {
        *outParam = new Els[2];

        for(int i = 0; i < (*outParam)->Length; i++)
            (*outParam)Idea = Els::Crash;
    }
}
-------------------------------- test.cpp --------------------------
2. The C# code that has infinite loop and calls method "TestMethodEnum" inside.
-------------------------------- test.cs --------------------------
using System;
using System.Collections;

using managedC;

namespace marshalBug
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            for(int i = 0; ; i++)
            {
                Els[] paramEnum;
                testClass.TestMethodArray(out paramEnum);

                foreach(Els t in paramEnum)
                    Console.WriteLine(t);

                GC.Collect();
            }
        }
    }
}
-------------------------------- test.cs --------------------------
This code works only one circle until GC.Collect(). The CG crashes.

Who can me say what is this Is this a bug in compiler
Are there anybody who have the same situalion

Thanks

ps:
Are there any fixes or SP for VC 2003



Answer this question

Compiler bug in VC++ .Net 2003

  • jkaater

    Hi Sergey: I've taken a look at this an the problem does appear to be related specifically to returning an array of enums as an out-parameter. The Visual C++ 2003 compiler has a bug in which it creates an array of System::Enum instead of an array of Els. I also tried your example with the current build of the Visual C++ 2005 compiler and it does not have the same problem - the code correctly compiles and runs and runs and runs Smile.

    There is not yet a SP release for Visual C++ 2003.

    Jonathan Caves
    Visual C++ Compiler Team

  • Compiler bug in VC++ .Net 2003