I was testing out the connection events (disconnect/reconnect) and got a NullReferenceException with no further information.
The registration of the events works fine. The exception occurs when I unplug a camera, so I assume it’s caused when calling the ‘Disconnected’ event handler. You can find my code below.
if (img == null || !INotify.CanNotify(img))
return;
long info;
int res = INotify.NOGetStatus(img, INotify.CVNotifyEventID.DEVICE_DISCONNECTED, INotify.CVNotifyInfoCmd.IS_AVAILABLE, out info);
if (res < 0 || info < 0)
return;
else
res = INotify.NORegister(img, INotify.CVNotifyEventID.DEVICE_DISCONNECTED, callback.OnDisconnected, IntPtr.Zero, out disconnectCookie);
res = INotify.NOGetStatus(img, INotify.CVNotifyEventID.DEVICE_RECONNECT, INotify.CVNotifyInfoCmd.IS_AVAILABLE, out info);
if (res < 0 || info < 0)
return;
else
res = INotify.NORegister(img, INotify.CVNotifyEventID.DEVICE_RECONNECT, callback.OnReconnected, IntPtr.Zero, out reconnectCookie);
The objects ‘img’ and ‘callback’ are not null when unplugging the camera.
The callstack does not show something from our library. This is native code.
Where do you save the delegates for the OnDisconnected and OnReconnected events?
These delegates MUST be stored as they are given to native code which is not visible for the garbage collector.
Then the garbage collector might release it which leads to a NullReferenceException.