Skip to content

App crashes when using Sensor.Start(FrameCallback) #1250

@baSSiLL

Description

@baSSiLL

Hi
I'm using D415 on PC with .NET wrapper.
I prefer push model for consuming frames. The only API I've found for that is Sensor.Start method taking a callback delegate. However, after calling it the app crashes suddenly (after processing a few frames) and debug output contains message about invoking a garbage-collected delegate.

Digging into the code revealed that Sensor.Start actually creates another ad-hoc delegate which it then passes to unmanaged code. But because this ad-hoc delegate is not referenced by any managed object it becomes a subject for garbage collection after leaving the method body. So, after it is collected, calls from unmanaged code lead to error.

To mitigate this issue, a reference to delegate should be stored somewhere. Some options include:

  • A private field in Sensor class. Storing a single delegate instance should be sufficient because no more than a single callback can be activated simultaneously. Care should be taken here regarding thread-safety.

  • Make the Start method return some token (a "black-box" data structure) which holds a reference to the delegate. And require to pass this token in a subsequent invocation of Stop method. This should force a responsible user of the class to hold the token (effectively keeping the delegate alive) for a period when the callback can be actually called.

For the time being I've worked the issue around by making a replacement method for Sensor.Start in my code which calls unmanaged rs2_start and does proper work of storing a reference to a delegate passed.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions