Native C++ Events Are Broken 
Author Message
 Native C++ Events Are Broken

You ought to ask this question on the dotnet.languages.vc group.
Andy.


Quote:
> Looking into this a little further (I discovered the /Fx compiler option),
> when you have multiple constructors it appears that only the first will
get
> the injected initialization code, and again only if it comes immediately
> after the class definition.

> So, the constructors here...

>     [event_source(native)]
>     class EventSource
>     {
>     public:
>         EventSource (void);
>         EventSource (int a);

>         __event void EventFoo (void);
>     };

>     EventSource::EventSource (void)
>     {
>     }

>     EventSource::EventSource (int a)
>     {
>     }

> ...look like this after code injection:

>     EventSource::EventSource (void)
>     {
>     //+++ Start Injected Code For Attribute 'event' from
> EventSource::EventSource
> #injected_line 279 "EventTest.cpp"
>     __eventHandlerList_EventSource_EventFoo = 0;

>     //--- End Injected Code For Attribute 'event'

>     }

>     EventSource::EventSource (int a)
>     {
>     }

> If they were defined inline with the class they would both get injected.

> Sure wish this stuff worked...
> -Jack (C# junkie who's being forced to work in C++)



> > Maybe I'm mistaken... you be the judge.  (I'm using v13.00.9466 of the
> > compiler.)

> <snip>



Fri, 21 Jan 2005 17:36:22 GMT  
 Native C++ Events Are Broken

Quote:
> You ought to ask this question on the dotnet.languages.vc group.
> Andy.

Looks like I hit the wrong newsgroup.

My original post follows:

---

Maybe I'm mistaken... you be the judge.  (I'm using v13.00.9466 of the
compiler.)

Take this simple example:

    [event_source(native)]
    class EventSource
    {
    public:
        EventSource (void);
        __event void EventFoo (void);
    };

    EventSource::EventSource (void)
    {
    }

This works.  It's one of the only cases I've found that does.

The event_source attribute injects a member called
'__eventHandlerList_EventSource_EventFoo' into the class and a couple of
instructions to initialize it to NULL in the constructor.  This is easily
verified by putting a breakpoint on the constructor and switching to
Disassembly.

     1:
     2:         //+++ Start Injected Code For Attribute 'event' from
EventSource::EventSource
     3: #injected_line 278 "EventTest.cpp"
     4:     __eventHandlerList_EventSource_EventFoo = 0;
0049B11A 8B 45 FC         mov         eax,dword ptr [this]
0049B11D C7 40 04 00 00 00 00 mov         dword ptr [eax+4],0

(At
ms-help://MS.VSCC/MS.MSDNVS/vccore/html/vcconintroductiontounifiedeventmodel
.htm there's a comment in the example code which says that the attribute is
optional for native C++.  This is obviously not true since it's the
attribute provider that injects this stuff.)

It's easy enough to verify that the event can be hooked and raised.  Let's
see if we can break it by adding a member function.

    [event_source(native)]
    class EventSource
    {
    public:
        EventSource (void);

        void Foo (void);

        __event void EventFoo (void);
    };

    EventSource::EventSource (void)
    {
    }

    void EventSource::Foo (void)
    {
    }

No, this works too.  Now try moving the Foo function up between the class
and the constructor.  Suddenly the injected initializer has vanished!  Try
raising the event and you'll likely get an exception.

You can try putting other things in between the class and the constructor
definition.  *Every* language construct that I've tried -- variable, namespa
ce, structure, even an extern -- either causes an internal compiler error or
it causes the eventHandlerList not to be initialized.  Put another way,
AFAICT the only thing that works is having the ctor inline in the class or
immediately afterwards.  That or the event source object is a global which
will hide the missing initialization.

The workaround of course is to put '__eventHandlerList_EventSource_EventFoo
= NULL' in the ctor.

I'll wager that if this was tested before release, it was tested using only
trivial cases like the above.  Sorry if I sound crabby but this is pretty
shoddy.
-Jack



Sat, 22 Jan 2005 02:37:10 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Native C++ Events Are Broken

2. Native C++ Events Are Broken

3. Is THINK C 5.0 broken, or am I?

4. event_source(native) not injecting event initialization code into constructor always

5. lint on Altos 2000 is BROKE BROKE BROKE!

6. Help a Newbie at Visual C++ (I am not a newbie at C++)

7. Event interface over the internet - am I INSANE?

8. Passing an __event in native C++

9. Using __event in native C++

10. no Intellisense with mixed Managed/Native C++

11. Visual C++ 7 supporting non .NET / Native code?

12. A couple of Win32 native C++ only MSVC7 questions

 

 
Powered by phpBB® Forum Software