-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add notice about known problems with SDL2 and V's garbage colle…
…ctor (vlang#745)
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// This file serves as a MRE (minimal reproducible example) of a runtime crash triggered by compiling and running | ||
// this file with just `v run ~/.vmodules/sdl/tests/crash_with_gc.vv`. On some setups, the problem seems to be | ||
// memory corruption happening between actions in SDL2's memory allocations and V's default garbage collector. | ||
// Especially when a lot of heap allocations occur. | ||
// | ||
// The example does not crash if compiled with `-d sdl_memory_no_gc`. | ||
// See also: https://github.com/vlang/sdl/issues/744 | ||
module main | ||
|
||
import sdl | ||
|
||
struct Data1 { | ||
mut: | ||
a int | ||
} | ||
|
||
fn main() { | ||
mut data1 := []&Data1{cap: 200} | ||
for i in 0 .. 200 { | ||
data1 << &Data1{ | ||
a: i | ||
} | ||
} | ||
|
||
sdl.init(sdl.init_video) | ||
window := sdl.create_window('Hello SDL2'.str, 300, 300, 500, 300, 0) | ||
renderer := sdl.create_renderer(window, -1, u32(sdl.RendererFlags.accelerated) | u32(sdl.RendererFlags.presentvsync)) | ||
|
||
mut should_close := false | ||
mut ticks := 0 | ||
for { | ||
ticks++ | ||
evt := sdl.Event{} | ||
for 0 < sdl.poll_event(&evt) { | ||
match evt.@type { | ||
.quit { should_close = true } | ||
else {} | ||
} | ||
} | ||
|
||
data1[0].a = ticks | ||
data1.delete(10) | ||
data1 << &Data1{ | ||
a: ticks | ||
} | ||
|
||
println('ticks: ${ticks}') | ||
if should_close || ticks == 1000 { | ||
break | ||
} | ||
|
||
sdl.set_render_draw_color(renderer, 255, 55, 55, 255) | ||
sdl.render_clear(renderer) | ||
sdl.render_present(renderer) | ||
} | ||
println('Exiting. If this was compiled without `-d sdl_memory_no_gc`, an invalid memory access error should occur') | ||
|
||
sdl.destroy_renderer(renderer) | ||
sdl.destroy_window(window) | ||
sdl.quit() | ||
} |