I'm accessing a private member of the class in the DoIt method. How can the GC collect the instance that is holding that before I access it. I thought the GC is only collecting if the instance is no longer reachable!
Correct me if I'm wrong!
Turns out this is only happens when interacting with PInvoke and unmanaged operations. Within managed code it doesn't happen.
I thought that this has to do with PInvoke. But I guess it has to do while the PInvoke code is running not before that!
Yes. It reminds me of one of the really hard-to-repro bugs I've ever seen which was in an app designed to "turn off" the Windows key while in certain applications.
Basically you have to interact with Windows and give them a delegate object which they call whenever a key is pressed, but sadly even delegate objects get garbage collected, at which point the next keystroke kills the application immediately. Took me weeks
to find that bug.