@@ -8,12 +8,11 @@ DWORD WINAPI TimeoutThreadExecutor(LPVOID lpParam) {
8
8
TimeoutData* threadData = (TimeoutData*)lpParam;
9
9
std::this_thread::sleep_for (std::chrono::milliseconds (threadData->timeout * 1000 ));
10
10
threadData->operation (threadData->data );
11
- free (threadData);
12
11
return 0 ;
13
12
}
14
13
#endif
15
14
16
- TimeoutOutputData setTimeout (int timeout, void * data, void (*operation)(void *)) {
15
+ TimeoutOutputData* setTimeout (int timeout, void * data, void (*operation)(void *)) {
17
16
std::thread::native_handle_type darwinHandle = nullptr ;
18
17
HANDLE windowsHandle = nullptr ;
19
18
struct TimeoutData * timeoutData = nullptr ;
@@ -43,27 +42,35 @@ TimeoutOutputData setTimeout(int timeout, void * data, void(*operation)(void*))
43
42
#endif
44
43
}
45
44
46
- return { darwinHandle, windowsHandle, timeoutData };
45
+ TimeoutOutputData* result = reinterpret_cast <TimeoutOutputData*>(malloc (sizeof (struct TimeoutOutputData )));
46
+ result->windowsHandle = windowsHandle;
47
+ result->darwinHandle = darwinHandle;
48
+ result->timeoutData = timeoutData;
49
+
50
+ return result;
47
51
}
48
52
49
- void clearTimeout (TimeoutOutputData data) {
50
- if (data. timeoutData ) {
51
- free (data. timeoutData );
52
- data. timeoutData = nullptr ;
53
+ void clearTimeout (TimeoutOutputData* data) {
54
+ if (data-> timeoutData ) {
55
+ free (data-> timeoutData );
56
+ data-> timeoutData = nullptr ;
53
57
}
54
58
55
59
#ifdef WIN32
56
- if (data. windowsHandle ) {
57
- int terminateThreadResult = TerminateThread (data. windowsHandle , 9 );
60
+ if (data-> windowsHandle ) {
61
+ int terminateThreadResult = TerminateThread (data-> windowsHandle , 9 );
58
62
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminatethread#return-value
59
63
if (terminateThreadResult == 0 )
60
64
{
61
65
DWORD errorCode = GetLastError ();
62
66
}
63
67
}
64
68
#else
65
- if (data. darwinHandle ) {
66
- pthread_cancel (data. darwinHandle );
69
+ if (data-> darwinHandle ) {
70
+ pthread_cancel (data-> darwinHandle );
67
71
}
68
72
#endif
73
+
74
+ free (data);
75
+ data = nullptr ;
69
76
}
0 commit comments