# How to debug a silent crash?

**URL:** https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383
**Category:** General Usage
**Tags:** crash
**Created:** [March 25, 2026, 3:16pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383 "2026-03-25T15:16:10Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [March 25, 2026, 3:16pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/1 "2026-03-25T15:16:10Z")

</div>

I’m writing a script which calls a C library and the program is just stopping prematurely with no output.

How do I debug this?

A little context:  
I’m passing a Julia function as a callback to the SimpleBLE C library that is called when data is received from a Bluetooth device. The callback is called once and seems to execute successfully and a few seconds later the process stops.  
The callback contains an `unsafe_wrap` to read the incoming data, then calls `println` to display it and finally calls the library’s free function on the received data.

---

<div class="post-metadata">

### Author: ![obsidianjulua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obsidianjulua/32/221495_2.png) [@obsidianjulua](https://discourse.julialang.org/u/obsidianjulua)
#### Post date: [March 25, 2026, 5:38pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/2 "2026-03-25T17:38:45Z")

</div>

Is julia garbage collecting the callback closure? You may need to setup some GC preserve, or flush the c buffer in order for julia to print thd output

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [March 25, 2026, 7:22pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/3 "2026-03-25T19:22:52Z")

</div>

Do you mean the callback itself can get garbage collected after C uses it exactly once?

Here is the offending function

```julia-auto
# The following functions call on each other in sequence
# This reformats the callback to only need to take one argument
function peripheral_notify(handle, service, characteristic, callback)
	function c_callback(handle, service, characteristic, data, data_length, userdata)
		jdata = unsafe_wrap(Vector{UInt8}, data, data_length)
		callback(jdata)
		simpleble_free(data)
	end
	peripheral_notify(handle, service, characteristic, c_callback, C_NULL)
end
# This converts the callback into a c function
function peripheral_notify(handle, service, characteristic, callback, userdata)
	c_callback = @cfunction($callback, Cvoid, (SBLEPeripheral, SBLEUUID, SBLEUUID, Ptr{UInt8},Csize_t, Ptr{Cvoid}))
	_peripheral_notify(handle, service, characteristic, c_callback, userdata)
end
# This calls ccall 
_peripheral_notify(handle, service, characteristic, callback, userdata) =
	@ccall simpleblepath.simpleble_peripheral_notify(
		handle::SBLEPeripheral,
		service::SBLEUUID,
		characteristic::SBLEUUID,
		callback::Ptr{Cvoid},
		userdata::Ptr{Cvoid})::SimpleBLE_Error

```

Adding a `GC.@preserve` for the callbacks does not seem to fix it.

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [March 25, 2026, 7:33pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/4 "2026-03-25T19:33:57Z")

</div>

Ok, removing the `simpleble_free` seems to have solved the issue, but the docs say I should free the memory given to callbacks so now I’m confused.

---

<div class="post-metadata">

### Author: ![obsidianjulua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obsidianjulua/32/221495_2.png) [@obsidianjulua](https://discourse.julialang.org/u/obsidianjulua)
#### Post date: [March 25, 2026, 8:08pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/5 "2026-03-25T20:08:05Z")

</div>

stash the cfunction pointer somewhere the GC can see it. $callback in @cfunction

```llm
const _active_callbacks = Dict{Ptr{Cvoid}, Base.CFunction}()

function peripheral_notify(handle, service, characteristic, callback, userdata)
    c_callback = @cfunction($callback, Cvoid, (SBLEPeripheral, SBLEUUID, SBLEUUID, Ptr{UInt8}, Csize_t, Ptr{Cvoid}))
    _active_callbacks[handle] = c_callback # prevent GC
    _peripheral_notify(handle, service, characteristic, c_callback, userdata)
end

```

---

<div class="post-metadata">

### Author: ![obsidianjulua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obsidianjulua/32/221495_2.png) [@obsidianjulua](https://discourse.julialang.org/u/obsidianjulua)
#### Post date: [March 25, 2026, 8:15pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/7 "2026-03-25T20:15:52Z")

</div>

Actually you might be freeing the memory twice, and causing a heap corruption, why not freeing it works.

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [March 25, 2026, 8:17pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/8 "2026-03-25T20:17:10Z")

</div>

Does not seem to have fixed it. But it seems like good practice so I’m keeping it.

---

<div class="post-metadata">

### Author: ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)
#### Post date: [March 25, 2026, 8:17pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/9 "2026-03-25T20:17:40Z")

</div>

I’m making a MWE to send to the author to ask him.

---

<div class="post-metadata">

### Author: ![obsidianjulua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obsidianjulua/32/221495_2.png) [@obsidianjulua](https://discourse.julialang.org/u/obsidianjulua)
#### Post date: [March 31, 2026, 3:35pm UTC](https://discourse.julialang.org/t/how-to-debug-a-silent-crash/136383/10 "2026-03-31T15:35:07Z")

</div>

1. Function returns a pointer — who owns it? Factory functions return owned pointers (caller must free). Accessor functions return borrowed pointers (caller must NOT free). Interior pointers have lifetime tied to a parent.
2. Function takes a pointer argument — does it consume it? Most functions borrow their arguments. Some sink functions take ownership (callee frees). The caller needs to know so it doesn’t double-free.

# obsidian\_notes
