Method pointer, mixed code and error C1001

H

hyd

Hello,
I use VS2005 - C++/CLI.
I have some kind of events in native C++. I want to raise .NET events
when my native C++ events occur.
So, I wrote some code for that but I have an C1001 error (An internal
error has occurred in the compiler).

Below, a piece of code that produces the same error :

***** CLR - Class Library project *****
--- Mixed.cpp ---

struct Handler
{
virtual void Call( int iI, double iD ) = 0;
};

template<class T, void (T::*TMethod)( int, double )>
struct THandler : public Handler
{
T* pT;

THandler( T* ipT )
{
pT = ipT;
}

virtual void Call( int iI, double iD )
{
(pT->*TMethod)( iI, iD );
}
};

class Event
{
Handler* pHandler;
public:
Event() : pHandler(0) {}

template<class T, void (T::*TMethod)( int, double )>
void Register( T* ipT )
{
if( pHandler ) delete pHandler;
pHandler = new THandler<T, TMethod>( ipT );
}

void Raise( int iI, double iD )
{
if( pHandler ) pHandler->Call( iI, iD );
}
};

class Foo
{
public:
Event MyEvent;

Foo(void) {}
virtual ~Foo(void) {}

void DoSomething()
{
MyEvent.Raise( 1, 2.3 );
MyEvent.Raise( 5, 8.4 );
MyEvent.Raise( 4, 4.2 );
}
};

class UseFoo
{
public:
Foo* pFoo;

UseFoo()
{
pFoo = new Foo();
pFoo->MyEvent.Register<UseFoo, &UseFoo::OnMyEvent>( this );
}

void OnMyEvent( int iI, double iD )
{
double d = iI * iD;
}
};

public ref class MUseFoo
{
internal:
UseFoo* pUseFoo;
public:
MUseFoo()
{
pUseFoo = new UseFoo();
}
virtual ~MUseFoo()
{
delete pUseFoo;
}
};

****************************************************

The compilation gives me the following error :

1>Managed.cpp
1>.\Managed.cpp(19) : fatal error C1001: An internal error has occurred
in the compiler.
1>(compiler file 'msc1.cpp', line 1392)
1> To work around this problem, try simplifying or changing the program
near the locations listed above.
1>Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more
information
1> .\Managed.cpp(18) : while compiling class template member
function 'void THandler<T,TMethod>::Call(int,double)'
1> with
1> [
1> T=UseFoo,
1> TMethod={&UseFoo::OnMyEvent,0}
1> ]
1> .\Managed.cpp(33) : see reference to class template
instantiation 'THandler<T,TMethod>' being compiled
1> with
1> [
1> T=UseFoo,
1> TMethod={&UseFoo::OnMyEvent,0}
1> ]
1> .\Managed.cpp(66) : see reference to function template
instantiation 'void Event::Register<UseFoo,{&UseFoo::OnMyEvent,0}>(T
*)' being compiled
1> with
1> [
1> T=UseFoo
1> ]

Without the MUseFoo class, the project compiles fine.
Anybody would have an idea about this problem ???

Thanks,
Hervé.
 
T

Tamas Demjen

hyd said:
I use VS2005 - C++/CLI.
I have some kind of events in native C++. I want to raise .NET events
when my native C++ events occur.
So, I wrote some code for that but I have an C1001 error (An internal
error has occurred in the compiler).

Below, a piece of code that produces the same error :

***** CLR - Class Library project *****

Looks like a bug to me. It compiles in a CLR command line project, but I
have the same error in a CLR class library project.

By the way, your Event class is leaking, it requires

~Event() { delete pHandler; }

I can't believe this, this seems to be completely unrelated, but guess
what, if I add that destructor, the error disappears!

Even though your code is wrong, it should not cause an internal compiler
error. I believe it's a compiler bug, as it should create a default
destructor for you. But in this case, you can be thankful for the error,
at least it pointed out a memory leak. :)

If you're interested about events, you may find my article helpful:
http://tweakbits.com/articles/events/index.html

Tom
 
H

hyd

Tamas Demjen a écrit :
Looks like a bug to me.

I think so.
It compiles in a CLR command line project, but I
have the same error in a CLR class library project.

Yes, it's the same thing for me.
By the way, your Event class is leaking, it requires

~Event() { delete pHandler; }

The exact code was :
~Event() { if( pHandler ) delete pHandler; }
but I had suppressed this line to shorten the example.
I can't believe this, this seems to be completely unrelated, but guess
what, if I add that destructor, the error disappears!

Yes, this problem is very unstable. You delete or add some lines and
the error disappears, then, you do other changes and the error
reappears. But when the program is more longer, the error doesn't
disappear any more.
Even though your code is wrong, it should not cause an internal compiler
error. I believe it's a compiler bug, as it should create a default
destructor for you. But in this case, you can be thankful for the error,
at least it pointed out a memory leak. :)

If you're interested about events, you may find my article helpful:
http://tweakbits.com/articles/events/index.html

Thanks for the article, it's a good document about the subject.
Finally, my complete solution is close to yours.

This night I have had a new idea to try to solve the problem. I will
try to write a
THandlerNET class for .NET receiver class.

Hervé.
 

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