Create new sensor manager cause exception


Im using the SDK code, I had few hardware problem that cause the camera to disconnect and reconnect again, in this case the sensor manager recognize 2 sensors and when i want to use it one of the sensor had disconnect. in this case i want to recover and set the sensor manager again.
Im trying to re-create the sensor manager class in recover function i want to activate:

void Recover()
delete m_sensorManager;
m_sensorManager = new SensorManager();
int numSensors = m_sensorManager->deviceCount();
for (int i = 0; i < numSensors; i++) {
m_sensor[i] = m_sensorManager->sensor(i);
bool ok = m_sensor[i]->open(,, params.dw, params.dh);
if (!ok) {
// Error
else {
// open sensor

for the line: “delete m_sensorManager;” i got an exception:

[Core.Platform] ***** Encountered fatal exception: Access Violation, PID 40224 ***

0# ImFusion::Platform::getLastWindowsErrorMessage in ImFusionCore
1# ImFusion::Platform::getLastWindowsErrorMessage in ImFusionCore
2# RtlIsGenericTableEmpty in ntdll
3# RtlRaiseException in ntdll
4# KiUserExceptionDispatcher in ntdll
5# RecFusion::Sensor::isOpen in RecFusionSDK
6# ImFusion::Stream::updateListenersData in ImFusionStream
7# ImFusion::RealSenseStream::captureThread in ImFusionRGBD
8# ImFusion::RealSenseStream::~RealSenseStream in ImFusionRGBD
9# ImFusion::FileSensor::~FileSensor in ImFusionRGBD
10# beginthreadex in ucrtbase
11# BaseThreadInitThunk in KERNEL32
12# RtlUserThreadStart in ntdll

any advice ?


In the error trace, it seems that the sensor-capturing activity is still going on:

5# RecFusion::Sensor::isOpen in RecFusionSDK
6# ImFusion::Stream::updateListenersData in ImFusionStream

So, my hypothesis is that SensorManager deletes Sensor objects with underlying data, but the streaming/capturing threads are not aware, and thus, the Sensor::isOpen throws an error (cannot work on invalid memory?).

To test it, before calling delete m_sensorManager;, make sure you call Sensor::stop() followed by Sensor::close() for each opened sensor (to make sure each sensor is closed correctly). In fact, I have encountered (maybe) a slightly connected issue here: Potential error in the RecFusion SDK 2.2.0 while closing many sensors

Please try the suggestion of Patryk to first delete all running sensors via calling Sensor::stop() and then Sensor::close() for each sensor. This is not done automatically when the SensorManager is deleted.