fix memory leak in SDL_Init()

we use a static guard to only call SDL_Init() once.

Another option would be to call SDL_Quit(), but it doesn't seem to be
doing anything.

Also, fix the HTML code and add 'use strict' directive.

BUG=webp:354

Change-Id: I3c6421e2c1c8cc200556cd4092a0ead9a8b054ef
This commit is contained in:
skal 2017-07-31 12:45:07 -07:00
parent 461ae5551b
commit 6878d42720
3 changed files with 23 additions and 9 deletions

View File

@ -28,6 +28,7 @@
#include <SDL/SDL.h>
#endif
static int init_ok = 0;
int WebpToSDL(const char* data, unsigned int data_size) {
int ok = 0;
VP8StatusCode status;
@ -42,7 +43,10 @@ int WebpToSDL(const char* data, unsigned int data_size) {
return 1;
}
if (!init_ok) {
SDL_Init(SDL_INIT_VIDEO);
init_ok = 1;
}
status = WebPGetFeatures((uint8_t*)data, (size_t)data_size, &config.input);
if (status != VP8_STATUS_OK) goto Error;
@ -97,6 +101,7 @@ int WebpToSDL(const char* data, unsigned int data_size) {
Error:
SDL_FreeSurface(surface);
SDL_FreeSurface(screen);
WebPFreeDecBuffer(output);
return ok;
}

View File

@ -12,12 +12,15 @@
<script type="text/javascript" src="./webp.js"></script>
<script type="text/javascript">
'use strict';
// main wrapper for the function decoding a WebP into a canvas object
var WebpToCanvas;
function init() {
WebpToCanvas = Module.cwrap('WebpToSDL', 'number', ['array', 'number']);
}
window.onload = init;
function decode(webp_data, canvas_id) {
// get the canvas to decode into
@ -27,10 +30,10 @@ function decode(webp_data, canvas_id) {
Module.canvas = canvas;
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
// decode and measure timing
start = new Date();
var start = new Date();
var ret = WebpToCanvas(webp_data, webp_data.length);
end = new Date();
speed_result = document.getElementById('timing');
var end = new Date();
var speed_result = document.getElementById('timing');
// display timing result
if (speed_result != null) {
var decode_time = end - start;
@ -53,7 +56,7 @@ function loadfile(filename, canvas_id) {
</script>
</head>
<body onload='init()'>
<body>
<p>
<strong>WebP in JavaScript demo</strong> -
</p>

View File

@ -11,6 +11,11 @@
</script>
<script type="text/javascript">
'use strict';
// main wrapper for the function decoding a WebP into a canvas object
var WebpToCanvas;
function init() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'webp_wasm.wasm', true);
@ -23,6 +28,7 @@ function init() {
};
xhr.send(null);
}
window.onload = init;
function decode(webp_data, canvas_id) {
var result;
@ -36,16 +42,16 @@ function decode(webp_data, canvas_id) {
Module.canvas = canvas;
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
// decode and measure timing
start = new Date();
var start = new Date();
var ret = WebpToCanvas(webp_data, webp_data.length);
end = new Date();
var end = new Date();
var decode_time = end - start;
result = 'decoding time: ' + decode_time +' ms.';
} else {
result = "WASM module not finished loading! Please retry";
}
// display timing result
speed_result = document.getElementById('timing');
var speed_result = document.getElementById('timing');
if (speed_result != null) {
speed_result.innerHTML = '<p>'+ result + '</p>';
}
@ -66,7 +72,7 @@ function loadfile(filename, canvas_id) {
</script>
</head>
<body onload='init()'>
<body>
<p>
<strong>WebP demo using Web-Assembly</strong> -
</p>