RenderKit / ospray

An Open, Scalable, Portable, Ray Tracing Based Rendering Engine for High-Fidelity Visualization
http://ospray.org
Apache License 2.0
1.01k stars 182 forks source link

ospShutdown results in EXC_BAD_ACCESS #440

Closed StasJ closed 4 years ago

StasJ commented 4 years ago

The following code will crash

ospInit(&argc, argv);
ospDeviceSetErrorFunc(ospGetCurrentDevice(), [](OSPError, const char*){});
ospShutdown();
Twinklebear commented 4 years ago

Hey @StasJ , the value returned by ospGetCurrentDevice() is actually a refcounted object, so when you call ospShutdown() and quit your program has a dangling reference to the device. Check out the example app's setup for setting the error functions and callbacks for how to do this: https://github.com/ospray/ospray/blob/master/apps/ospExamples/example_common.h#L12-L48 . In short, you'll want to hang onto the handle returned by ospGetCurrentDevice and then release it when you're done. So here you'd want to do:

ospInit(&argc, argv);
OSPDevice handle = ospGetCurrentDevice();
ospDeviceSetErrorFunc(handle, [](OSPError, const char*){});
ospRelease(handle);
ospShutdown()
Twinklebear commented 4 years ago

Though actually, since the object is refcounted internally I'd expect it to be cleaned up ok in the local rendering case. So the crash may be actually related to https://github.com/ospray/ospray/issues/441 as well, which will have a similar effect of leaving the device dangling by not calling ospShutdown

johguenther commented 4 years ago

With ospDeviceRelease(deviceHandle) the crash is prevented. Or better using cpp-wrappers cpp::Device::current() instead ospGetCurrentDevice() , which automatically releases the device.