|
6 | 6 |
|
7 | 7 | #include "matleap.h" |
8 | 8 |
|
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 | +} |
11 | 27 |
|
12 | 28 | /// @brief process interface arguments |
13 | 29 | /// |
@@ -85,7 +101,7 @@ mxArray *create_and_fill (const Leap::Vector &v) |
85 | 101 | void get_frame (int nlhs, mxArray *plhs[]) |
86 | 102 | { |
87 | 103 | // get the frame |
88 | | - const matleap::frame &f = fg.get_frame (); |
| 104 | + const matleap::frame &f = fg->get_frame (); |
89 | 105 | // create matlab frame struct |
90 | 106 | const char *frame_field_names[] = |
91 | 107 | { |
@@ -130,11 +146,18 @@ void get_frame (int nlhs, mxArray *plhs[]) |
130 | 146 |
|
131 | 147 | void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) |
132 | 148 | { |
| 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 | + } |
133 | 156 | switch (get_command (nlhs, plhs, nrhs, prhs)) |
134 | 157 | { |
135 | 158 | // turn on debug |
136 | 159 | case -1: |
137 | | - fg.set_debug (true); |
| 160 | + fg->set_debug (true); |
138 | 161 | return; |
139 | 162 | // get version |
140 | 163 | case 0: |
|
0 commit comments