Skip to content

Commit

Permalink
fix the way Leap::Controller is allocated/deleted so that it works on…
Browse files Browse the repository at this point in the history
… Windows.
  • Loading branch information
jeffsp committed Nov 19, 2013
1 parent 501c717 commit ea1939f
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions matleap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,24 @@

#include "matleap.h"

// Global instance
matleap::frame_grabber fg;
// Under Windows, a Leap::Controller must be allocated after the MEX
// startup code has completed. Also, a Leap::Controller must be
// deleted in the function specified by mexAtExit after all global
// destructors are called. If the Leap::Controller is not allocated
// and freed in this way, the MEX function will crash and cause MATLAB
// to hang or close abruptly. Linux and OS/X don't have these
// constraints, and you can just create a global Leap::Controller
// instance.

// Global instance pointer
matleap::frame_grabber *fg = 0;

// Exit function
void matleap_exit ()
{
delete fg;
fg = 0;
}

/// @brief process interface arguments
///
Expand Down Expand Up @@ -85,7 +101,7 @@ mxArray *create_and_fill (const Leap::Vector &v)
void get_frame (int nlhs, mxArray *plhs[])
{
// get the frame
const matleap::frame &f = fg.get_frame ();
const matleap::frame &f = fg->get_frame ();
// create matlab frame struct
const char *frame_field_names[] =
{
Expand Down Expand Up @@ -130,11 +146,18 @@ void get_frame (int nlhs, mxArray *plhs[])

void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (!fg)
{
fg = new matleap::frame_grabber;
if (fg == 0)
mexErrMsgTxt ("Cannot allocate a frame grabber");
mexAtExit (matleap_exit);
}
switch (get_command (nlhs, plhs, nrhs, prhs))
{
// turn on debug
case -1:
fg.set_debug (true);
fg->set_debug (true);
return;
// get version
case 0:
Expand Down

0 comments on commit ea1939f

Please sign in to comment.