Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public EdgeEvent GetEvent(ulong index)
return CallLibgpiod(() =>
{
using EdgeEventNotFreeable edgeEventHandle = LibgpiodV2.gpiod_edge_event_buffer_get_event(Handle, index);
// gpiod_edge_event_buffer_get_event returns NULL when the requested index is not populated in the buffer. Passing a null pointer
// to gpiod_edge_event_copy triggers a native assertion (assert(event)) which aborts the process. Guard against it here.
if (edgeEventHandle.IsInvalid)
{
throw new GpiodException($"Edge event at index {index} is not populated in the buffer.");
}

// Since events are tied to the buffer instance, different threads may not operate on the buffer and any associated events at the same
// time. Events can be copied using ::gpiod_edge_event_copy in order to create a standalone objects - which each may safely be used from
// a different thread concurrently.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,15 @@ private void HandleEdgeEventsOfRequestInLoop(LineRequest request)

int numberOfReadEvents = request.ReadEdgeEvents(edgeEventBuffer);

for (int i = 0; i < numberOfReadEvents; i++)
// Bound iteration by the number of events actually stored in the buffer. The value returned by ReadEdgeEvents can exceed the
// number of events retrievable from the buffer, in which case GetEvent would receive a null event from libgpiod.
int numberOfBufferedEvents = edgeEventBuffer.GetNumEvents();
int numberOfEventsToHandle = Math.Min(numberOfReadEvents, numberOfBufferedEvents);

for (int i = 0; i < numberOfEventsToHandle; i++)
{
EdgeEvent edgeEvent = edgeEventBuffer.GetEvent((ulong)i);
HandleEdgeEvent(edgeEvent);
using EdgeEvent edgeEvent = edgeEventBuffer.GetEvent((ulong)i);
HandleEdgeEvent(edgeEvent);
}
}
}
Expand Down
Loading