我在使用CImGui.jl时发现一个特殊的问题。为了描述,我写了如下的代码:
module TestBugs
using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.ImGuiGLFWBackend
using CImGui.ImGuiOpenGLBackend
using CImGui.ImGuiGLFWBackend.LibGLFW
using CImGui.ImGuiOpenGLBackend.ModernGL
using CImGui.LibCImGui
using CImGui.LibCImGui: ImGuiTextFilter_PassFilter
export UI
function UI()
glfwDefaultWindowHints()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2)
if Sys.isapple()
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) # 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
end
# create window
window = glfwCreateWindow(1280, 720, "TestBugs", C_NULL, C_NULL)
@assert window != C_NULL
glfwMakeContextCurrent(window)
glfwSwapInterval(1) # enable vsync
# create OpenGL and GLFW context
window_ctx = ImGuiGLFWBackend.create_context(window)
gl_ctx = ImGuiOpenGLBackend.create_context()
# setup Dear ImGui context
ctx = CImGui.CreateContext()
# enable docking and multi-viewport
global io = CImGui.GetIO()
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_DockingEnable
io.ConfigFlags = unsafe_load(io.ConfigFlags) | CImGui.ImGuiConfigFlags_ViewportsEnable
# io.ConfigDockingWithShift = true
# setup Platform/Renderer bindings
ImGuiGLFWBackend.init(window_ctx)
ImGuiOpenGLBackend.init(gl_ctx)
@async try
clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
global glfwwindowx = Cint(0)
global glfwwindowy = Cint(0)
while glfwWindowShouldClose(window) == 0
glfwPollEvents()
ImGuiOpenGLBackend.new_frame(gl_ctx)
ImGuiGLFWBackend.new_frame(window_ctx)
CImGui.NewFrame()
filterbug()
CImGui.Render()
glfwMakeContextCurrent(window)
width, height = Ref{Cint}(), Ref{Cint}() #! need helper fcn
glfwGetFramebufferSize(window, width, height)
display_w = width[]
display_h = height[]
glViewport(0, 0, display_w, display_h)
glClearColor(clear_color...)
glClear(GL_COLOR_BUFFER_BIT)
ImGuiOpenGLBackend.render(gl_ctx)
if unsafe_load(igGetIO().ConfigFlags) & ImGuiConfigFlags_ViewportsEnable == ImGuiConfigFlags_ViewportsEnable
backup_current_context = glfwGetCurrentContext()
igUpdatePlatformWindows()
GC.@preserve gl_ctx igRenderPlatformWindowsDefault(C_NULL, pointer_from_objref(gl_ctx))
glfwMakeContextCurrent(backup_current_context)
end
glfwSwapBuffers(window)
GC.safepoint()
yield()
end
catch e
@error "[$(now())]\nError in renderloop!" exception = e
Base.show_backtrace(stderr, catch_backtrace())
finally
ImGuiOpenGLBackend.shutdown(gl_ctx)
ImGuiGLFWBackend.shutdown(window_ctx)
CImGui.DestroyContext(ctx)
glfwDestroyWindow(window)
end
end
let
filter::Ptr{ImGuiTextFilter} = ImGuiTextFilter_ImGuiTextFilter(C_NULL)
content = ["ajfa;", "qoe;asdjk", "xnm,d", "178250kd", "g078gyu", "awefr23"]
global function filterbug()
CImGui.Begin("TestBugs")
ImGuiTextFilter_Draw(filter, "Filter contents", 16CImGui.GetFontSize())
for s in content
ImGuiTextFilter_PassFilter(filter, pointer(s), C_NULL) || continue
CImGui.Text(s)
end
CImGui.End()
end
end
end # module TestBugs
如果运行方式是
include("path/to/file/TestBugs.jl")
TestBugs.UI()
那么一切都可以正常使用
但是,当我把它制作成一个包,按照下述方式运行则会报错
using Pkg
Pkg.activate("path/to/package/TestBugs")
using TestBugs
UI()
错误信息
Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x7ff983700a01 -- strlen at C:\WINDOWS\System32\msvcrt.dll (unknown line)
in expression starting at none:0
strlen at C:\WINDOWS\System32\msvcrt.dll (unknown line)
_ZN5ImGui11InputTextExEPKcS1_PciRK6ImVec2iPFiP26ImGuiInputTextCallbackDataEPv at C:\Users\22112\.julia\artifacts\332c8d1e20d65e18db01d6e29164ca87a9d83cb0\bin\libcimgui.dll (unknown line)
_ZN5ImGui9InputTextEPKcPcyiPFiP26ImGuiInputTextCallbackDataEPv at C:\Users\22112\.julia\artifacts\332c8d1e20d65e18db01d6e29164ca87a9d83cb0\bin\libcimgui.dll (unknown line)
_ZN15ImGuiTextFilter4DrawEPKcf at C:\Users\22112\.julia\artifacts\332c8d1e20d65e18db01d6e29164ca87a9d83cb0\bin\libcimgui.dll (unknown line)
ImGuiTextFilter_Draw at C:\Users\22112\.julia\packages\LibCImGui\DfBwq\lib\x86_64-w64-mingw32.jl:5027 [inlined]
filterbug at C:\Users\22112\OneDrive - mails.ucas.ac.cn\鏂囨。\CODE\Julia\MyPackages\TestBugs\src\TestBugs.jl:99
macro expansion at C:\Users\22112\OneDrive - mails.ucas.ac.cn\鏂囨。\CODE\Julia\MyPackages\TestBugs\src\TestBugs.jl:57 [inlined]
#1 at .\task.jl:514
unknown function (ip: 0000028cfd649673)
jl_apply at C:/workdir/src\julia.h:1879 [inlined]
start_task at C:/workdir/src\task.c:1092
Allocations: 1947758 (Pool: 1945834; Big: 1924); GC: 3
我发现这种行为不一致在很多情况中都有出现(之前在使用RemoteChannel时也出现问题),但是我没有在哪里看到指出两者区别的文档。所以借此,我想了解这两种运行方式的区别到底有那些?