Bugs (fixed):
* Startup crash w/ error message: "A callback was made on a garbage
collected delegate of type ApiNativeMethods.raw_pb_callback"
Cause: The callback is only referenced in unmanaged c++. Without references
in managed code, the Garbage Collector deems the delegate eligible for
collection before the callback has finished execution.
Note: The original solution forced a reference in managed code by storing it
in a class-member 'keepalive' dictionary, whose life isn't bound to the
[synchronous] scope of the 'send' method. The callback would then remove
itself from the dictionary in its last line of code. While this reduced the
chance of such an error occurring, occassional crashes persisted
(particularly in low-bandwidth environments). I suspect the frequency at
which garbage-collector threads run is inversely related to the frequency
at which data arrives from the server, as the latter triggers execution of
higher-priority threads.
Steps to repro:
1. Insert a Thread.Sleep statement (1000 ms should be sufficient) after
the call that removes the last reference to the delegate (i.e., make
the thread sleep for a significant amount of time in the callback
AFTER it becomes eligible for garbage collection).
2. Build, run, and observe crash on startup.
Solution: Wrap the callback in another class, and store the wrapper
in the dictionary rather than the callback itself. The reason why this
works isn't totally clear, however its effectiveness has been verified
with the repro (described above).