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

BeginTabItem's parameter p_open cannot be null reference #495

Open
WilliamQiufeng opened this issue Sep 25, 2024 · 0 comments
Open

BeginTabItem's parameter p_open cannot be null reference #495

WilliamQiufeng opened this issue Sep 25, 2024 · 0 comments

Comments

@WilliamQiufeng
Copy link

In ImGui, you can pass a null pointer to p_open so that the tab item is always shown and the selection is managed by imgui itself. However, here

if (ImGui.BeginTabItem("a", Unsafe.NullRef<bool>(), ImGuiTabItemFlags.None))
   ImGui.Text("Hi");
   ImGui.EndTabItem();
}

Will simply make it not render, as opposed to passing a ref b where b is a boolean variable.

This comes down to the handling of the reference, since in the method it dereferences it no matter what. The following modified code worked for me:

    public static unsafe bool BeginTabItem(string label, ref bool p_open, ImGuiTabItemFlags flags)
        {
            byte* native_label;
            int label_byteCount = 0;
            if (label != null)
            {
                label_byteCount = Encoding.UTF8.GetByteCount(label);
                if (label_byteCount > Util.StackAllocationSizeLimit)
                {
                    native_label = Util.Allocate(label_byteCount + 1);
                }
                else
                {
                    byte* native_label_stackBytes = stackalloc byte[label_byteCount + 1];
                    native_label = native_label_stackBytes;
                }
                int native_label_offset = Util.GetUtf8(label, native_label, label_byteCount);
                native_label[native_label_offset] = 0;
            }
            else { native_label = null; }

            byte ret;
            if (Unsafe.IsNullRef(ref p_open))
            {
                ret = ImGuiNative.igBeginTabItem(native_label, (byte*)0, flags);
            }
            else
            {
                byte native_p_open_val = p_open ? (byte)1 : (byte)0;
                byte* native_p_open = &native_p_open_val;
                ret = ImGuiNative.igBeginTabItem(native_label, native_p_open, flags);
                p_open = native_p_open_val != 0;
            }
            if (label_byteCount > Util.StackAllocationSizeLimit)
            {
                Util.Free(native_label);
            }
            return ret != 0;
        }
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

1 participant