Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# ArgumentError: embedded NULs are not allowed in C strings: #5

Closed
zygmuntszpak opened this issue Apr 15, 2019 · 4 comments
Closed

Comments

@zygmuntszpak
Copy link

Hi,
I'm having trouble figuring out how to use CImGui.InputText(). The examples in the demo work correctly, but whenever I try to incorporate something similar I encounter an embedded NULs are not allowed in C strings: error. For example, the following does not work

        buf="dummy"*"\0"^(27) 
        CImGui.InputText("dummy", buf, length(buf))  # ArgumentError: embedded NULs are not allowed in C strings

I've made a standalone script which attempts to imitate an example in the demos:

using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
using Printf


@static if Sys.isapple()
    # OpenGL 3.2 + GLSL 150
    glsl_version = 150
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
    GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
else
    # OpenGL 3.0 + GLSL 130
    glsl_version = 130
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
    # GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    # GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
end

# setup GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)

# create window
window = GLFW.CreateWindow(1280, 720, "Demo")
@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1)  # enable vsync

# setup Dear ImGui context
ctx = CImGui.CreateContext()

# setup Dear ImGui style
CImGui.StyleColorsDark()

# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)
should_show_dialog = true
clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
current_path = pwd()
while !GLFW.WindowShouldClose(window)

    GLFW.PollEvents()
    # start the Dear ImGui frame
    ImGui_ImplOpenGL3_NewFrame()
    ImGui_ImplGlfw_NewFrame()
    CImGui.NewFrame()

    # ArgumentError: embedded NULs are not allowed in C strings:
    @cstatic buf="dummy"*"\0"^(27) begin
        CImGui.InputText("1", buf, length(buf))
    end

    # rendering
    CImGui.Render()
    GLFW.MakeContextCurrent(window)
    display_w, display_h = GLFW.GetFramebufferSize(window)
    glViewport(0, 0, display_w, display_h)
    glClearColor(clear_color...)
    glClear(GL_COLOR_BUFFER_BIT)
    ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())

    GLFW.MakeContextCurrent(window)
    GLFW.SwapBuffers(window)
end

# cleanup
ImGui_ImplOpenGL3_Shutdown()
ImGui_ImplGlfw_Shutdown()
CImGui.DestroyContext(ctx)

GLFW.DestroyWindow(window)

This error is as follows:

ERROR: LoadError: ArgumentError: embedded NULs are not allowed in C strings: "dummy\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
Stacktrace:
 [1] igInputText(::String, ::String, ::Int64, ::Int64, ::Ptr{Nothing}, ::Ptr{Nothing}) at ./c.jl:216
 [2] InputText at /home/zygmunt/.julia/packages/CImGui/PuVOE/src/wrapper.jl:1298 [inlined] (repeats 2 times)
 [3] top-level scope at /home/zygmunt/.julia/dev/ElectrodermalActivity/src/standalone.jl:59 [inlined]
 [4] top-level scope at ./none:0
in expression starting at /home/zygmunt/.julia/dev/ElectrodermalActivity/src/standalone.jl:49

I suspect that I am using the API incorrectly. I haven't yet worked with a scenario where I was passing data structures to a C-library so I'd appreciate any suggestions you may have.

@zygmuntszpak
Copy link
Author

Here is how I worked around the problem. I have three working solutions, but I am not sure which (if any) are "permissible". I would go with option 3 because it is the shortest:

        # Solution 1
        str = "dummy"*"\0"^(27)
        buf = Cstring(Base.unsafe_convert(Ptr{Cchar}, str))
        CImGui.InputText("Label 1",buf, length(str))
        @show str

        # Solution 2
        str2 =  "dummy"*"\0"^(27)
        buf2 = Cstring(pointer(Base.cconvert(Ptr{Cchar}, str2)))
        CImGui.InputText("Label 2",buf2, length(str2))
        @show str2

        # Solution 3
        str3 =  "dummy"*"\0"^(27)
        buf3 = Cstring(pointer(str3))
        CImGui.InputText("Label 3",buf3, length(str3))
        @show str3

@Gnimuc
Copy link
Owner

Gnimuc commented Apr 15, 2019

Sorry, that's an upgrading flaw due to this commit.

@Gnimuc Gnimuc closed this as completed in 2b6c886 Apr 15, 2019
@Gnimuc
Copy link
Owner

Gnimuc commented Apr 15, 2019

Thanks for the feedback! I'm going to tag a new version.

@wdevore
Copy link

wdevore commented Apr 22, 2019

Here is how I worked around the problem. I have three working solutions, but I am not sure which (if any) are "permissible". I would go with option 3 because it is the shortest:

        # Solution 1
        str = "dummy"*"\0"^(27)
        buf = Cstring(Base.unsafe_convert(Ptr{Cchar}, str))
        CImGui.InputText("Label 1",buf, length(str))
        @show str

        # Solution 2
        str2 =  "dummy"*"\0"^(27)
        buf2 = Cstring(pointer(Base.cconvert(Ptr{Cchar}, str2)))
        CImGui.InputText("Label 2",buf2, length(str2))
        @show str2

        # Solution 3
        str3 =  "dummy"*"\0"^(27)
        buf3 = Cstring(pointer(str3))
        CImGui.InputText("Label 3",buf3, length(str3))
        @show str3

Weird. I get the opposite. The examples get an error and I don't know how to fix it. I am on a linux box with julia 1.1.0. I will try one of zygmuntszpak solutions.

Update: I upgraded to 1.69.1 and the issue is fixed. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants