Skip to content

Commit 08c19ed

Browse files
committed
Move (static) thread local last_error buffer into lsl_last_error function to avoid a MinGW bug
1 parent b25e6e7 commit 08c19ed

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/common.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <mmsystem.h>
1717
#endif
1818

19-
thread_local char last_error[512] = {0};
20-
2119
int64_t lsl::lsl_local_clock_ns() {
2220
return std::chrono::nanoseconds(std::chrono::steady_clock::now().time_since_epoch()).count();
2321
}
@@ -43,7 +41,10 @@ LIBLSL_C_API void lsl_destroy_string(char *s) {
4341
if (s) free(s);
4442
}
4543

46-
LIBLSL_C_API const char *lsl_last_error(void) { return last_error; }
44+
LIBLSL_C_API const char *lsl_last_error(void) {
45+
thread_local char last_error[LAST_ERROR_SIZE] = {0};
46+
return last_error;
47+
}
4748
}
4849

4950
// === implementation of misc functions ===

src/common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ extern "C" {
3030
"Please do not compile this with a lslboost version older than 1.45 because the library would otherwise not be protocol-compatible with builds using other versions."
3131
#endif
3232

33-
extern thread_local char last_error[512];
34-
3533
// the highest supported protocol version
3634
// * 100 is the original version, supported by library versions 1.00+
3735
// * 110 is an alternative protocol that improves throughput, supported by library versions 1.10+
@@ -40,6 +38,9 @@ const int LSL_PROTOCOL_VERSION = 110;
4038
// the library version
4139
const int LSL_LIBRARY_VERSION = 115;
4240

41+
/// size of the lsl_last_error() buffer size
42+
const int LAST_ERROR_SIZE = 512;
43+
4344
namespace lsl {
4445
/// A very large time duration (> 1 year) for timeout values.
4546
const double FOREVER = 32000000.0;

src/lsl_c_api_helpers.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
/// Helper for LSL_STORE_EXCEPTION_IN
88
#define LSLCATCHANDSTORE(ecvar, Exception, code) \
99
catch (Exception & e) { \
10-
strncpy(last_error, e.what(), sizeof(last_error) - 1); \
10+
strncpy(const_cast<char *>(lsl_last_error()), e.what(), LAST_ERROR_SIZE - 1); \
1111
int32_t *__ec = ecvar; \
1212
if (__ec != nullptr) *__ec = code; \
1313
}
1414

1515
/// Helper for LSL_STORE_EXCEPTION_IN
1616
#define LSLCATCHANDRETURN(Exception, code) \
1717
catch (Exception & e) { \
18-
strncpy(last_error, e.what(), sizeof(last_error) - 1); \
18+
strncpy(const_cast<char *>(lsl_last_error()), e.what(), LAST_ERROR_SIZE - 1); \
1919
return code; \
2020
}
2121

0 commit comments

Comments
 (0)