Skip to content

Commit a2bf9f2

Browse files
committed
Fix for #13
1 parent 4202cb9 commit a2bf9f2

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/core.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ extern "C" {
1111

1212
using namespace cv;
1313

14+
// Fix for https://github.com/sparkfun/micropython-opencv/issues/13
15+
//
16+
// TLDR; The CoreTLSData object gets allocated once, whenever the first OpenCV
17+
// function that needs it happens to be called. That will only happen from the
18+
// user's code, after the GC has been initialized, meaning it gets allocated on
19+
// the GC heap (see `__wrap_malloc()`). If a soft reset occurs, the GC gets
20+
// reset and overwrites the memory location, but the same memory location is
21+
// still referenced for the CoreTLSData object, resulting in bogus values and
22+
// subsequent `CV_Assert()` calls fail
23+
//
24+
// The solution here is to create a global variable that subsequently calls
25+
// `getCoreTlsData()` to allocate the CoreTLSData object before the GC has
26+
// been initialized, so it gets allocated on the C heap and persists through
27+
// soft resets. `getCoreTlsData()` is not publicly exposed, but `theRNG()` is
28+
// exposed, which just runs `return getCoreTlsData().rng`
29+
volatile RNG rng = theRNG();
30+
1431
mp_obj_t cv2_core_convertScaleAbs(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
1532
// Define the arguments
1633
enum { ARG_src, ARG_dst, ARG_alpha, ARG_beta };

0 commit comments

Comments
 (0)