locked
What's the difference between ::add() and += in event handling?!

    Question

  • Wow, I've tracked my problem down (I think) and it turns out that using :add() to add event handlers to events is buggy to hell. In one scenario, the event didn't even fire at all and in that other case, it was flinging around null references. I've changed ALL event handler registrations to += and suddenly I have no issues at all.

    Shouldn't ::add() and += do exactly the same?

    --edit: Initial problem was this... Fixed now.

    In my application, I'm creating a bunch of Platform::Array's in a loop and send them flying across a bunch of nested async lambda methods. At random points, the ref object goes missing (in the debugger, its pointer is suddenly NULL, when the app breaks on an error). Sometimes, the same problem causes the app to tank in vccorlib causing access violations. Is this a known problem?

    • Edited by Tom Servo Thursday, May 10, 2012 7:25 PM problem found
    Thursday, May 10, 2012 6:30 PM

All replies

  • Hello Tom,

    Your new question is not really clear to me. I guess "ref object goes missing" is the statement that I'm getting hung up on. Can you clarify?

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, May 11, 2012 12:41 AM
    Moderator
  •  I pass an object to a function via an event handler, said function then runs code in a lambda via ThreadPool::RunAsync or CoreDispatcher::RunAsync. Sometimes, not always (which is the confusing thing), the object isn't available anymore within the lambda and points to nullptr. That's what I mean with bugs in reference counting.

    What I seem to have found is that this happens when the event handler is registered with ::add(). I changed it to += and suddenly my problems went away. This seems strange, because I assume that += is/should be syntactic sugar for ::add(). I only noticed because another event handler wouldn't even fire until I changed the appropriate handler registration from ::add() to +=.

    When back home, I'all try to create a skeleton project to reproduce this.

    --edit: Well, it's not event related either. There's strange things happening here. It goes as far as me storing a reference as protected variable inside the class, and it still ends up being nullptr sometimes. I'm on a wild goose chase here. :(

    • Edited by Tom Servo Friday, May 11, 2012 1:37 PM
    Friday, May 11, 2012 6:23 AM
  • Tom,

    If you can create a small project to reproduce the issue and put it on SkyDrive I can take a look at it using the internal source code and see if I can shed some light on this very strange problem for you.

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, May 11, 2012 11:00 PM
    Moderator