Skip to content

Commit 4d6b941

Browse files
committed
fix(c_sync): handle error codes to close #627
Signed-off-by: Benn Snyder <[email protected]>
1 parent d913755 commit 4d6b941

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

wrappers/c_sync/libfreenect_sync.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,18 @@ static void *init(void *unused)
214214
return NULL;
215215
}
216216

217-
static void init_thread(void)
217+
static int init_thread(void)
218218
{
219219
thread_running = 1;
220-
freenect_init(&ctx, 0);
220+
int ret = freenect_init(&ctx, 0);
221+
if (ret != 0) return ret;
221222
// We claim both the motor and the camera, because we can't know in advance
222223
// which devices the caller will want, and the c_sync interface doesn't
223224
// support audio, so there's no reason to claim the device needlessly.
224225
freenect_select_subdevices(ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
225-
pthread_create(&thread, NULL, init, NULL);
226+
ret = pthread_create(&thread, NULL, init, NULL);
227+
if (ret != 0) return ret;
228+
return 0;
226229
}
227230

228231
static int change_video_format(sync_kinect_t *kinect, freenect_resolution res, freenect_video_format fmt)
@@ -279,8 +282,10 @@ static int setup_kinect(int index, int res, int fmt, int is_depth)
279282
pending_runloop_tasks_inc();
280283
pthread_mutex_lock(&runloop_lock);
281284
int thread_running_prev = thread_running;
282-
if (!thread_running)
283-
init_thread();
285+
if (!thread_running) {
286+
int ret = init_thread();
287+
if (ret != 0) return ret;
288+
}
284289
if (!kinects[index]) {
285290
kinects[index] = alloc_kinect(index);
286291
}
@@ -334,15 +339,15 @@ static int sync_get(void **data, uint32_t *timestamp, buffer_ring_t *buf)
334339
}
335340

336341

337-
/*
342+
/*
338343
Use this to make sure the runloop is locked and no one is in it. Then you can
339344
call arbitrary functions from libfreenect.h in a safe way. If the kinect with
340-
this index has not been initialized yet, then it will try to set it up. If
345+
this index has not been initialized yet, then it will try to set it up. If
341346
this function is successful, then you can access kinects[index]. Don't forget
342347
to unlock the runloop when you're done.
343-
348+
344349
Returns 0 if successful, nonzero if kinect[index] is unvailable
345-
*/
350+
*/
346351
static int runloop_enter(int index)
347352
{
348353
if (index < 0 || index >= MAX_KINECTS) {
@@ -352,7 +357,7 @@ static int runloop_enter(int index)
352357
if (!thread_running || !kinects[index])
353358
if (setup_kinect(index, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT, 1))
354359
return -1;
355-
360+
356361
pending_runloop_tasks_inc();
357362
pthread_mutex_lock(&runloop_lock);
358363
return 0;
@@ -407,7 +412,7 @@ int freenect_sync_get_tilt_state(freenect_raw_tilt_state **state, int index)
407412
{
408413
if (runloop_enter(index)) return -1;
409414
freenect_update_tilt_state(kinects[index]->dev);
410-
*state = freenect_get_tilt_state(kinects[index]->dev);
415+
*state = freenect_get_tilt_state(kinects[index]->dev);
411416
runloop_exit();
412417
return 0;
413418
}

0 commit comments

Comments
 (0)