Skip to content

SDL creating window corrupts heap when compiled with -g #15489

@yosmo78

Description

@yosmo78

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

#ifndef EverythingH
#define EverythingH
#include <emscripten.h>
#include <SDL.h>

#define GL_GLEXT_PROTOTYPES 1
#include <SDL_opengles2.h>

#include <string>


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

extern Everything e;

#endif

main.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

#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

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
    <!-- Create the canvas that the C++ code will draw into -->
    <canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>

    <!-- Allow the C++ to access the canvas element --> 
    <script type='text/javascript'>
        var canv = document.getElementById('canvas');
        var Module = {
            canvas: canv
        };
    </script>
    
    <!-- Call the javascript glue code (index.js) as generated by Emscripten -->
    <script src="index.js"></script>

</body>

</html>

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions