Skip to content

Commit

Permalink
lib: add + use initial shy_free (part of long term plan for better …
Browse files Browse the repository at this point in the history
…tracing memory bugs)
  • Loading branch information
larpon committed Oct 8, 2023
1 parent f96dddb commit 02c9c0f
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 28 deletions.
4 changes: 2 additions & 2 deletions lib/alarms.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ fn (mut a Alarms) reset() ! {
fn (mut a Alarms) shutdown() ! {
for alarm in a.active {
unsafe {
free(alarm)
shy_free(alarm)
}
}
for alarm in a.pool {
unsafe {
free(alarm)
shy_free(alarm)
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions lib/animation.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ pub fn (mut a Anims) shutdown() ! {
a.shy.assert_api_shutdown()
for anim in a.active {
unsafe {
free(anim)
if isnil(anim) {
a.shy.log.gerror('${@MOD}.${@STRUCT}', 'TODO V memory bug ${a.active.len}')
return
}
shy_free(anim)
}
}
for anim in a.f32pool {
unsafe {
free(anim)
if isnil(anim) {
a.shy.log.gerror('${@MOD}.${@STRUCT}', 'TODO V memory bug ${a.f32pool.len}')
return
}
shy_free(anim)
}
}
}
Expand All @@ -89,8 +97,8 @@ pub fn (mut a Anims) update(dt f64) {
animator := a.active[i]
// TODO(lmp) workaround weird crash/behavior with `-d shy_analyse` here?!?
if isnil(animator) {
a.shy.log.gerror('${@MOD}.${@STRUCT}','TODO :( ${a.active.len}')
return //continue
a.shy.log.gerror('${@MOD}.${@STRUCT}', 'TODO V memory bug ${a.active.len}')
return
}

if animator.kind == .follow {
Expand Down
18 changes: 9 additions & 9 deletions lib/api.default.v
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ pub fn (mut a ShyAPI) shutdown() ! {
fn (mut a ShyAPI) free() {
a.shy.log.gdebug('${@STRUCT}.${@FN}', '')
unsafe {
free(a.input)
free(a.scripts)
free(a.assets)
free(a.draw)
free(a.gfx)
free(a.audio)
free(a.system)
free(a.wm)
free(a.events)
shy_free(a.input)
shy_free(a.scripts)
shy_free(a.assets)
shy_free(a.draw)
shy_free(a.gfx)
shy_free(a.audio)
shy_free(a.system)
shy_free(a.wm)
shy_free(a.events)
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/input.b.v
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ fn (ip Input) sdl_to_shy_event(sdl_event sdl.Event) Event {
path := unsafe { cstring_to_vstring(dropped_path) }
defer {
// sdl.free(dropped_path) // TODO Crashes?!
unsafe { free(dropped_path) }
unsafe { shy_free(dropped_path) }
}
shy_event = DropFileEvent{
timestamp: timestamp
Expand All @@ -408,7 +408,7 @@ fn (ip Input) sdl_to_shy_event(sdl_event sdl.Event) Event {
text := unsafe { cstring_to_vstring(dropped_text) }
defer {
// sdl.free(dropped_text) // TODO Crashes?!
unsafe { free(dropped_text) }
unsafe { shy_free(dropped_text) }
}
shy_event = DropTextEvent{
timestamp: timestamp
Expand Down
4 changes: 2 additions & 2 deletions lib/input.v
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ pub fn (k &Keyboard) is_key_down(keycode KeyCode) bool {
pub fn (mut k Keyboard) set_key_state(key_code KeyCode, button_state ButtonState) {
match button_state {
.up {
k.keys[i32(key_code)] = false
k.keys[int(key_code)] = false
}
.down {
k.keys[i32(key_code)] = true
k.keys[int(key_code)] = true
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/script.v
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ pub fn (mut sc Scripts) shutdown() ! {

for mut wvm in sc.wren_vms {
wvm.shutdown()!
unsafe { free(wvm) }
unsafe { shy_free(wvm) }
}
}
9 changes: 9 additions & 0 deletions lib/shy.api.v
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@ pub fn size(w f32, h f32) Size {
height: h
}
}

[inline; markused; unsafe]
pub fn shy_free(ptr voidptr) {
assert !isnil(ptr), 'shy_free tries to free null pointer'
unsafe { free(ptr) }
unsafe {
ptr = nil
}
}
2 changes: 1 addition & 1 deletion lib/shy.v
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn run[T](mut ctx T, config Config) ! {

ctx.shutdown()!
shy_instance.shutdown()!
unsafe { free(shy_instance) }
unsafe { shy_free(shy_instance) }
}

fn (s Shy) health() ! {
Expand Down
4 changes: 2 additions & 2 deletions lib/timer.v
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ pub fn (mut t Timers) shutdown() ! {
t.shy.assert_api_shutdown()
for timer in t.active {
unsafe {
free(timer)
shy_free(timer)
}
}
for timer in t.pool {
unsafe {
free(timer)
shy_free(timer)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/window.sdl.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn (w Window) dump_pixels(rect Rect) &Screenshot {
[unsafe]
pub fn (mut ss Screenshot) free() {
unsafe {
free(ss.pixels)
shy_free(ss.pixels)
ss.pixels = &u8(0)
}
}
Expand All @@ -131,5 +131,5 @@ pub fn (mut ss Screenshot) free() {
[unsafe]
pub fn (mut ss Screenshot) destroy() {
unsafe { ss.free() }
unsafe { free(ss) }
unsafe { shy_free(ss) }
}
6 changes: 3 additions & 3 deletions lib/window.sdl.v
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pub fn (mut w Window) set_icon(source AssetSource) ! {
})!

sdl.set_window_icon(w.handle, surf)
unsafe { free(surf) }
unsafe { shy_free(surf) }
}

[inline]
Expand Down Expand Up @@ -712,10 +712,10 @@ pub fn (mut w Window) shutdown() ! {
window.close()!
}
w.anims.shutdown()!
unsafe { free(w.anims) }
unsafe { shy_free(w.anims) }

w.timers.shutdown()!
unsafe { free(w.timers) }
unsafe { shy_free(w.timers) }

w.set_current()

Expand Down

0 comments on commit 02c9c0f

Please sign in to comment.