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

View File

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

View File

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