Skip to content

Commit f42ca51

Browse files
authored
all: make GC opt-in via sdl_use_gc, remove -d sdl_memory_no_gc (#848)
1 parent 9d22c7f commit f42ca51

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ Also note that SDL2 **is not** compatible with SDL `v1.x`.
5959
## Notes on garbage collection and memory issues
6060

6161
Currently, with some setups, SDL2 is known to trigger crashes when used in conjunction
62-
with V's default garbage collector. In these cases running apps importing `sdl` with
63-
`v run` you may experience runtime crashes and output similar to this:
62+
with V's default garbage collector. Because of this you have to explicitly **opt-in**
63+
to use V's garbage collection with SDL2.
64+
65+
If you choose to use the garbage collector with SDL objects
66+
(by running apps importing `sdl` with `v -d sdl_use_gc run`)
67+
you may experience runtime crashes and output similar to this:
6468

6569
```
6670
main__main: RUNTIME ERROR: invalid memory access
6771
```
6872

6973
We are tracking the issue here: https://github.com/vlang/sdl/issues/744
7074

71-
The crashes can be avoided by passing `-d sdl_memory_no_gc` when compiling V applications
72-
that contains `import sdl` and managing SDL2's memory manually with calls to the various
73-
`destroy` and `sdl.free/1` functions.
75+
The crashes can be avoided by simply **not** passing `-d sdl_use_gc` and
76+
managing memory manually with SDL's memory functions like `sdl.free/1`, `sdl.malloc/1`,
77+
`sdl.calloc/2`, `sdl.realloc/2` and the various `create_*` and `destroy` functions.
7478

7579
## Support
7680

@@ -131,9 +135,9 @@ To do this, change to the root directory of the sdl module, like
131135
`cd %HOMEPATH%\.vmodules\sdl`
132136
and run
133137
`v run windows_install_dependencies.vsh`.
134-
This will create a directory called "thirdparty" which will be used to download and extract the required libraries.
135-
To successfully run a provided example or your own projects, the sdl dlls must be copied to the main application directory.
136-
e.g.:
138+
This will create a directory called "thirdparty" which will be used to download and
139+
extract the required libraries. To successfully run a provided example or your own projects,
140+
the sdl dlls must be copied to the main application directory. e.g.:
137141
```bash
138142
copy thirdparty\SDL2-2.30.0\lib\x64\SDL2.dll examples\basic_window\
139143
cd ..

c/memory.c.v

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module c
44
// that Boehm will later know how to process. The callbacks here provide such versions:
55
fn cb_malloc_func(size usize) voidptr {
66
mut res := unsafe { nil }
7-
$if sdl_memory_no_gc ? {
8-
res = unsafe { C.malloc(size) }
9-
} $else {
7+
$if sdl_use_gc ? {
108
res = unsafe { malloc(int(size)) }
9+
} $else {
10+
res = unsafe { C.malloc(size) }
1111
}
1212
$if trace_sdl_memory ? {
1313
C.fprintf(C.stderr, c'>> sdl.c.cb_malloc_func | size: %lu | => %p\n', size, res)
@@ -17,10 +17,10 @@ fn cb_malloc_func(size usize) voidptr {
1717

1818
fn cb_calloc_func(nmemb usize, size usize) voidptr {
1919
mut res := unsafe { nil }
20-
$if sdl_memory_no_gc ? {
21-
res = unsafe { C.calloc(int(nmemb), int(size)) }
22-
} $else {
20+
$if sdl_use_gc ? {
2321
res = unsafe { vcalloc(isize(nmemb) * isize(size)) }
22+
} $else {
23+
res = unsafe { C.calloc(int(nmemb), int(size)) }
2424
}
2525
$if trace_sdl_memory ? {
2626
C.fprintf(C.stderr, c'>> sdl.c.cb_calloc_func | nmemb: %lu | size: %lu | => %p\n',
@@ -31,10 +31,10 @@ fn cb_calloc_func(nmemb usize, size usize) voidptr {
3131

3232
fn cb_realloc_func(mem voidptr, size usize) voidptr {
3333
mut res := unsafe { nil }
34-
$if sdl_memory_no_gc ? {
35-
res = unsafe { C.realloc(&u8(mem), int(size)) }
36-
} $else {
34+
$if sdl_use_gc ? {
3735
res = unsafe { v_realloc(&u8(mem), isize(size)) }
36+
} $else {
37+
res = unsafe { C.realloc(&u8(mem), int(size)) }
3838
}
3939
$if trace_sdl_memory ? {
4040
C.fprintf(C.stderr, c'>> sdl.c.cb_realloc_func | mem: %p | size: %lu | => %p\n',
@@ -47,10 +47,10 @@ fn cb_free_func(mem voidptr) {
4747
$if trace_sdl_memory ? {
4848
C.fprintf(C.stderr, c'>> sdl.c.cb_free_func | mem: %p\n', mem)
4949
}
50-
$if sdl_memory_no_gc ? {
51-
unsafe { C.free(mem) }
52-
} $else {
50+
$if sdl_use_gc ? {
5351
unsafe { free(mem) }
52+
} $else {
53+
unsafe { C.free(mem) }
5454
}
5555
}
5656

tests/crash_with_gc.vv

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This file serves as a MRE (minimal reproducible example) of a runtime crash triggered by compiling and running
2-
// this file with just `v run ~/.vmodules/sdl/tests/crash_with_gc.vv`. On some setups, the problem seems to be
2+
// this file with `v -d sdl_use_gc run ~/.vmodules/sdl/tests/crash_with_gc.vv`. On some setups, the problem seems to be
33
// memory corruption happening between actions in SDL2's memory allocations and V's default garbage collector.
44
// Especially when a lot of heap allocations occur.
55
//
6-
// The example does not crash if compiled with `-d sdl_memory_no_gc`.
6+
// The example crashes if compiled with `-d sdl_use_gc`.
77
// See also: https://github.com/vlang/sdl/issues/744
88
module main
99

@@ -53,7 +53,7 @@ fn main() {
5353
sdl.render_clear(renderer)
5454
sdl.render_present(renderer)
5555
}
56-
println('Exiting. If this was compiled without `-d sdl_memory_no_gc`, an invalid memory access error should occur')
56+
println('Exiting. If this was compiled with `-d sdl_use_gc`, an invalid memory access error should occur')
5757

5858
sdl.destroy_renderer(renderer)
5959
sdl.destroy_window(window)

0 commit comments

Comments
 (0)