#Odin program terminates without further info when doing allocations in a callback proc

1 messages · Page 1 of 1 (latest)

spiral monolith
#

Odin terminates as soon as I'm trying to do allocations in a callback proc when in interop with a C library. Without giving further info.

It is simple as make([]byte, 128) or fmt.tprint calls that terminate the program.

Unfortunately, I had a dead end on this a few months ago and gave up on it.
Now, trying to look into it again its stil that Odin seems like a offended little bitch not telling me anything.
(No offense meant)

Step by step repro, creating a tiny project (tested on Gnu/Linux):

odin_gui_proj
├── webui
│   └── ...
└── main.odin
git init odin_gui_proj && cd odin_gui_proj
git submodule add https://github.com/webui-dev/odin-webui.git webui
webui/setup.sh
package main

import "core:fmt"
import "core:strconv"
import "core:strings"
import ui "webui"

DOC :: `<!DOCTYPE html>
<html>
    <head>
        <script src="webui.js"></script>
        <style>
            body {
                background: linear-gradient(to right, #179BFF, #005493);
                color: AliceBlue;
                font-family: monospace;
                text-align: center;
                margin-top: 30px;
            }
        </style>
        <button id="exit">Exit</button>
        <body>
            <h1>Hellope</h1>
        </body>
    </head>
</html>`


main :: proc() {
    w := ui.new_window()

    ui.bind(
        w,
        "exit",
        proc(_: ^ui.Event) {
            // Terminator alloc
            // buf := make([]byte, 128)
            // defer delete(buf)

            // str := fmt.tprintf("%s", "Just want a string ;(")
            // fmt.println(str)

            fmt.println("Bye!")
            ui.exit()
        },
    )

    ui.show(w, DOC)
    ui.wait()
}

Run this thing and click the exit button.
Run this thing with one of the alloc commented out and click the button again.
I'm running into this issue only with Odin.

upper zodiac
#

Is that webui package a binding you made?

#

If it is another language binding, callbacks usually don't have the Odin calling convention, so your callback type should be like proc "c" (_: ^ui.Event) {

#

Saying, hey this is called from a c codebase

#

then you will get a compiler error when you try to allocate because you need a context, it is now probably crashing because it is assumed to be there due to the wrong binding

#

The easiest way to do that is context = runtime.default_context() somewhere at the beginning of your callback

#

(with import "base:runtime")

spiral monolith
#

Thanks for the info, that's some reference points I'm going to check out

upper zodiac
#

I also recommend investing in setting up a debugger, it makes these segfaults/crashes much easier to debug

spiral monolith
#

I would love that. For what I found so far it's super hard to debug in Odin.

#

Is there some recommended way or documentation on how to do it?

upper zodiac
#

Not any harder than other c-like languages, you can use any debugger that supports c-like with Odin too (same debug information format)

#

You just do odin build with the -debug flag and you get an executable with debug info, you can then point any debugger like lldb, gdb, xcode, visual studio, remedy etc. to it and it should be good to go

spiral monolith
#

tyvm
gonna try the recommendations in regards to the callback now