emscripten-core / emscripten-core/emscripten

SDL creating window corrupts heap when compiled with -g

Open
#15,489 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

I am trying to use a singleton pattern in my rendering pipeline. But SDL doesn't like it when I compile without optimizations and it corrupt the heap. Here is the code:

header.h
```cpp
#ifndef EverythingH
#define EverythingH
#include
#include

#define GL_GLEXT_PROTOTYPES 1
#include

#include

class Everything
{
public:
void init();
void doStuff();
};

extern Everything e;

#endif
```

main.cpp
```cpp
#include "header.h"

void MainLoop()
{
e.doStuff();
}

void InitSingletons()
{
e.init();
}

int main()
{
//Init singletons here
InitSingletons();

//main loop is started here
emscripten_set_main_loop( MainLoop, 0, true );

return 0;
}
```

Everything.cpp
```cpp
#include "header.h"
EMSCRIPTEN_KEEPALIVE SDL_Window *SDLWindow; //I tried it without EMSCRIPTEN_KEEPALIVE

void Everything::init()
{
SDL_CreateWindowAndRenderer(640, 480, 0, &SDLWindow, nullptr);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
}
void Everything::doStuff()
{

}
EMSCRIPTEN_KEEPALIVE Everything e; //I tried it without EMSCRIPTEN_KEEPALIVE
```

index.html
```html





var canv = document.getElementById('canvas');
var Module = {
canvas: canv
};



```

compile command
```
em++ main.cpp Everything.cpp -std=c++11 -g -s WASM=1 -s USE_SDL=2 -o index.js
```

python server to run
```bash
python -m http.server 8080
```
then connect to 127.0.0.1:8080

Errors:
```
index.js:4384 emscripten_set_main_loop_timing: Cannot set timing mode for main loop since a main loop does not exist! Call emscripten_set_main_loop first to set one up.

Aborted(Runtime error: The application has corrupted its heap memory area (address zero)!)

Uncaught (in promise) RuntimeError: Aborted(Runtime error: The application has corrupted its heap memory area (address zero)!)
```

Info `em++ -v` gives
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.34 (0d24418f0eac4828f096ee070dae8472d427edaa)
clang version 14.0.0 (https://github.com/llvm/llvm-project 3d39612b3dd3f6b67ee63da305d30606abbe7287)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:/emsdk/upstream/bin
```

Edit:
in fact just compile the following with -g instead of -O3 to get the error
```
https://github.com/timhutton/opengl-canvas-wasm
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.