mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
stbi: fix memory leak from stbi.load/1
with GC (#16028)
This commit is contained in:
parent
34f233c93f
commit
5047058595
@ -263,6 +263,15 @@ fn get_all_commands() []Command {
|
||||
rmfile: 'v.c'
|
||||
}
|
||||
}
|
||||
$if linux {
|
||||
res << Command{
|
||||
line: '$vexe vlib/v/tests/bench/bench_stbi_load.v && prlimit -v10485760 vlib/v/tests/bench/bench_stbi_load'
|
||||
okmsg: 'STBI load does not leak with GC on, when loading images multiple times (use < 10MB)'
|
||||
runcmd: .execute
|
||||
contains: 'logo.png 1000 times.'
|
||||
rmfile: 'vlib/v/tests/bench/bench_stbi_load'
|
||||
}
|
||||
}
|
||||
$if !windows {
|
||||
res << Command{
|
||||
line: '$vexe -raw-vsh-tmp-prefix tmp vlib/v/tests/script_with_no_extension'
|
||||
|
15
thirdparty/stb_image/stbi.c
vendored
15
thirdparty/stb_image/stbi.c
vendored
@ -1,5 +1,19 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
extern void * stbi__callback_malloc(size_t size);
|
||||
extern void * stbi__callback_realloc(void *ptr, size_t size);
|
||||
extern void stbi__callback_free(void *ptr);
|
||||
|
||||
#define STBI_MALLOC(sz) stbi__callback_malloc(sz)
|
||||
#define STBI_REALLOC(p,newsz) stbi__callback_realloc(p,newsz)
|
||||
#define STBI_FREE(p) stbi__callback_free(p)
|
||||
#define STBIW_MALLOC(sz) stbi__callback_malloc(sz)
|
||||
#define STBIW_REALLOC(p,newsz) stbi__callback_realloc(p,newsz)
|
||||
#define STBIW_FREE(p) stbi__callback_free(p)
|
||||
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_write.h"
|
||||
|
||||
@ -20,3 +34,4 @@ void write_force_png_filter(int level){
|
||||
void write_tga_with_rle(int level) {
|
||||
stbi_write_tga_with_rle = level;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,31 @@
|
||||
|
||||
module stbi
|
||||
|
||||
[if trace_stbi_allocations ?]
|
||||
fn trace_allocation(message string) {
|
||||
eprintln(message)
|
||||
}
|
||||
|
||||
[export: 'stbi__callback_malloc']
|
||||
fn cb_malloc(s usize) voidptr {
|
||||
res := unsafe { malloc(isize(s)) }
|
||||
trace_allocation('> stbi__callback_malloc: $s => ${ptr_str(res)}')
|
||||
return res
|
||||
}
|
||||
|
||||
[export: 'stbi__callback_realloc']
|
||||
fn cb_realloc(p voidptr, s usize) voidptr {
|
||||
res := unsafe { v_realloc(p, isize(s)) }
|
||||
trace_allocation('> stbi__callback_realloc: ${ptr_str(p)} , $s => ${ptr_str(res)}')
|
||||
return res
|
||||
}
|
||||
|
||||
[export: 'stbi__callback_free']
|
||||
fn cb_free(p voidptr) {
|
||||
trace_allocation('> stbi__callback_free: ${ptr_str(p)}')
|
||||
unsafe { free(p) }
|
||||
}
|
||||
|
||||
#flag -I @VEXEROOT/thirdparty/stb_image
|
||||
#include "stb_image.h"
|
||||
#include "stb_image_write.h"
|
||||
|
19
vlib/v/tests/bench/bench_stbi_load.v
Normal file
19
vlib/v/tests/bench/bench_stbi_load.v
Normal file
@ -0,0 +1,19 @@
|
||||
import os
|
||||
import stbi
|
||||
|
||||
fn load_image(path string) {
|
||||
img := stbi.load(path) or { return }
|
||||
assert img.ok
|
||||
// unsafe { img.free() }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
pid := os.getpid()
|
||||
image_path := os.args[1] or { '${@VEXEROOT}/examples/assets/logo.png' }
|
||||
for i in 1 .. 1001 {
|
||||
if i % 100 == 0 {
|
||||
println('pid: $pid | Loaded $image_path $i times.')
|
||||
}
|
||||
load_image(image_path)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user