Hi Sam,
IIRC, when I started with Emscripten a while ago the program would abort in case of a memory error. As my app is comparable to a desktop app, this was not acceptable, so I set ABORTING_MALLOC to 0. I understand that this flag has a different meaning today. Here is how all my allocation calls work:
Error_T allocMemPtr(MemPtr_T *p,uint32_T size,boolean_T clear) {
_MemPtr_T mp;
if (clear)
mp = (_MemPtr_T)calloc(1,size + sizeof(_Mem_T));
else
mp = (_MemPtr_T)malloc(size + sizeof(_Mem_T));
if (mp) {
mp->size = size;
*p = (MemPtr_T)((char_T*)mp + sizeof(_Mem_T));
return kErr_NoErr;
}
return kErr_MemErr;
}
Error_T setMemPtrSize(MemPtr_T *p,uint32_T size){
_MemPtr_T m = _MP(*p);
MemPtr_T newPtr;
newPtr = realloc(m,size + sizeof(_Mem_T));
if (newPtr) {
m = (_MemPtr_T)newPtr;
m->size = size;
*p = (MemPtr_T)((char_T*)m + sizeof(_Mem_T));
return kErr_NoErr;
}
return kErr_MemErr;
}
So I should catch all errors. However, errors (i.e. return value == 0) are not reported by malloc or calloc during the problems I am experiencing. I added debug lines, but not a single failure was recorded.
Removing ABORTING_MALLOC did not result in any change of error outcome.
I see two different behaviors now:
- setting up workers and checking that they run by
static void startUpWorker(void) {
#ifdef __EMSCRIPTEN__
int32_T w = emscripten_wasm_worker_self_id();
if (! emscripten_current_thread_is_wasm_worker()){
EM_ASM_({
console.log("Error: No worker: " + $0);
},w);
}
#endif //__EMSCRIPTEN__
}
- then I do my stuff and receive about 10 of the "Uncaught RuntimeError: memory access out of bounds" errors.
- no failures of malloc/calloc recognized
The second behavior is
- in main() I call this routine:
static void memtest(void) {
#define NUM_CHUNKS 15
const int CHUNK_SIZE = 100 * 1024 * 1024;
int i;
void* p[NUM_CHUNKS];
Error_T err = kErr_NoErr;
for (int i = 0; i < NUM_CHUNKS; i++) {
err = allocMemPtr(&p[i],CHUNK_SIZE,FALSE); //see function above
if (err != kErr_NoErr || p[i] == NULLPTR) {
printf("Error chunk %d\n",i);
break;
}
}
for (int i = 0; i < NUM_CHUNKS; i++) {
if (p[i] == NULLPTR)
break;
disposeMemPtr(p[i]);
}
}
- then I start up the workers as described above
- then I do my stuff
- sometimes this results in error free behavior, but not always. If an error occurs, I only get one "Uncaught RuntimeError" message.
I am pretty confident that I handle memory allocation correctly, because my background is in development of desktop apps in C for 30+ years, and there you better not have any leaks and keep the app running whenever possible. So I must be doing something wrong when dealing with multiple threads.
I will try out pthreads next, because I have no idea anymore what the cause could be here.
Cheers,
Dieter