Skip to content

Commit ebf7896

Browse files
gh9869827davidplowman
authored andcommitted
Fix incorrect Hailo reference counting and virtual device reuse
Previously, TARGET_REF_COUNT was only incremented during the first virtual device allocation, but not for additional model instances. This caused the VDevice to be deallocated prematurely when multiple models were loaded, as each model called close() and decremented the count. Additionally, TARGET was not reset to None after releasing the virtual device, which caused reuse of a released virtual device and raised exceptions on subsequent model creation. This commit moves the TARGET_REF_COUNT increment outside the allocation block, and resets TARGET to None after the virtual device is released when the count reaches zero. Addresses #1300 Signed-off-by: Florent Gastoud (main) <[email protected]>
1 parent 46fa351 commit ebf7896

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

picamera2/devices/hailo/hailo.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def __init__(self, hef_path, batch_size=None, output_type='FLOAT32'):
2525
self.hef = HEF(hef_path)
2626
if Hailo.TARGET is None:
2727
Hailo.TARGET = VDevice(params)
28-
Hailo.TARGET_REF_COUNT += 1
28+
# increment `TARGET_REF_COUNT` every time a model is loaded
29+
Hailo.TARGET_REF_COUNT += 1
2930
self.target = Hailo.TARGET
3031
self.infer_model = self.target.create_infer_model(hef_path)
3132
self.infer_model.set_batch_size(1 if batch_size is None else batch_size)
@@ -184,3 +185,6 @@ def close(self):
184185
Hailo.TARGET_REF_COUNT -= 1
185186
if Hailo.TARGET_REF_COUNT == 0:
186187
self.target.release()
188+
# Reset `TARGET` to None so that subsequent __init__ calls can create
189+
# a new VDevice, since this one has been released
190+
Hailo.TARGET = None

0 commit comments

Comments
 (0)