Skip to content

Commit ea1939f

Browse files
committed
fix the way Leap::Controller is allocated/deleted so that it works on Windows.
1 parent 501c717 commit ea1939f

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

matleap.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@
66

77
#include "matleap.h"
88

9-
// Global instance
10-
matleap::frame_grabber fg;
9+
// Under Windows, a Leap::Controller must be allocated after the MEX
10+
// startup code has completed. Also, a Leap::Controller must be
11+
// deleted in the function specified by mexAtExit after all global
12+
// destructors are called. If the Leap::Controller is not allocated
13+
// and freed in this way, the MEX function will crash and cause MATLAB
14+
// to hang or close abruptly. Linux and OS/X don't have these
15+
// constraints, and you can just create a global Leap::Controller
16+
// instance.
17+
18+
// Global instance pointer
19+
matleap::frame_grabber *fg = 0;
20+
21+
// Exit function
22+
void matleap_exit ()
23+
{
24+
delete fg;
25+
fg = 0;
26+
}
1127

1228
/// @brief process interface arguments
1329
///
@@ -85,7 +101,7 @@ mxArray *create_and_fill (const Leap::Vector &v)
85101
void get_frame (int nlhs, mxArray *plhs[])
86102
{
87103
// get the frame
88-
const matleap::frame &f = fg.get_frame ();
104+
const matleap::frame &f = fg->get_frame ();
89105
// create matlab frame struct
90106
const char *frame_field_names[] =
91107
{
@@ -130,11 +146,18 @@ void get_frame (int nlhs, mxArray *plhs[])
130146

131147
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
132148
{
149+
if (!fg)
150+
{
151+
fg = new matleap::frame_grabber;
152+
if (fg == 0)
153+
mexErrMsgTxt ("Cannot allocate a frame grabber");
154+
mexAtExit (matleap_exit);
155+
}
133156
switch (get_command (nlhs, plhs, nrhs, prhs))
134157
{
135158
// turn on debug
136159
case -1:
137-
fg.set_debug (true);
160+
fg->set_debug (true);
138161
return;
139162
// get version
140163
case 0:

0 commit comments

Comments
 (0)