question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Segfault when typing in a TextBox (Avalonia 11 only)

See original GitHub issue

Describe the bug Typing anything in a text box produces a segmentation fault. It happens on Avalonia 11 only (release / RC2 / RC1), not an issue on previous Avalonia versions.

To reproduce

  1. Create a minimal Avalonia 11 project (see avalonia11-textbox-segfault.zip)
  2. Compile it (debug / release, it doesn’t matter) and run the resulting binary
  3. Type something in the text box
  4. Nothing happens, the program freezes for a second and then segfaults

Expected behavior The text box works as before in Avalonia 0.10, the text is displayed in the text box as typed, no segfault.

Screenshots avalonia11-textbox-segfault

Platform / Versions:

  • Distro: Arch Linux
  • Kernel: Linux 6.4.1-arch2-1 #1 SMP PREEMPT_DYNAMIC Tue, 04 Jul 2023 08:39:40 +0000 x86_64 GNU/Linux
  • Avalonia: Avalonia 11.0.0 (happens with RC2 and RC1 too)

Additional context I just ported a lot of code from Avalonia 0.10 to Avalonia 11, everything went well, except the aforementioned issue, because of which the porting changes had to be reverted (so back to Avalonia 0.10 for the moment).

Coredump info

           PID: 75155 (AvaloniaApplica)
           UID: 1000 (luc)
           GID: 984 (users)
        Signal: 11 (SEGV)
     Timestamp: Wed 2023-07-05 16:31:15 EEST (53s ago)
  Command Line: ./AvaloniaApplication1
    Executable: /home/luc/work/local/priv/test/AvaloniaApplication1/AvaloniaApplication1/bin/Debug/net7.0/AvaloniaApplication1
 Control Group: /user.slice/user-1000.slice/user@1000.service/app.slice/app-org.kde.krusader-e3fd96ebaf274139877d3c45a337602b.scope
          Unit: user@1000.service
     User Unit: app-org.kde.krusader-e3fd96ebaf274139877d3c45a337602b.scope
         Slice: user-1000.slice
     Owner UID: 1000 (luc)
       Boot ID: 2d88b41373eb4c939484ea0f6639dc2a
    Machine ID: 7ef120f7400d4e32b31e544f08d6ddf2
      Hostname: luc
       Storage: /var/lib/systemd/coredump/core.AvaloniaApplica.1000.2d88b41373eb4c939484ea0f6639dc2a.75155.1688563875000000.zst (present)
  Size on Disk: 13.6M
       Message: Process 75155 (AvaloniaApplica) of user 1000 dumped core.
                
                Stack trace of thread 75155:
                #0  0x00007f5dca97c004 Xutf8LookupString (libX11.so.6 + 0x4f004)
                #1  0x00007f5d5cc13721 n/a (n/a + 0x0)
                #2  0x00007f5d5cc13561 n/a (n/a + 0x0)
                #3  0x00007f5d5cc10ac3 n/a (n/a + 0x0)
                #4  0x00007f5d5c9c45fd n/a (n/a + 0x0)
                #5  0x00007f5d5cc063e8 n/a (n/a + 0x0)
                #6  0x00007f5d5c9a40e2 n/a (n/a + 0x0)
                #7  0x00007f5d5c9a3879 n/a (n/a + 0x0)
                #8  0x00007f5d5c9a3429 n/a (n/a + 0x0)
                #9  0x00007f5d5c9a310f n/a (n/a + 0x0)
                #10 0x00007f5d5c527140 n/a (n/a + 0x0)
                #11 0x00007f5d5b0a2443 n/a (n/a + 0x0)
                #12 0x00007f5d5b0a0acd n/a (n/a + 0x0)
                #13 0x00007f5dd97643e7 n/a (libcoreclr.so + 0x3643e7)
                #14 0x00007f5dd959bec4 n/a (libcoreclr.so + 0x19bec4)
                #15 0x00007f5dd9486254 n/a (libcoreclr.so + 0x86254)
                #16 0x00007f5dd948659a n/a (libcoreclr.so + 0x8659a)
                #17 0x00007f5dd94b2265 n/a (libcoreclr.so + 0xb2265)
                #18 0x00007f5dd9473317 coreclr_execute_assembly (libcoreclr.so + 0x73317)
                #19 0x00007f5dda0a9156 n/a (libhostpolicy.so + 0x15156)
                #20 0x00007f5dda0a943e n/a (libhostpolicy.so + 0x1543e)
                #21 0x00007f5dda0a9d96 corehost_main (libhostpolicy.so + 0x15d96)
                #22 0x00007f5dda0f072b n/a (libhostfxr.so + 0x1072b)
                #23 0x00007f5dda0ef74c n/a (libhostfxr.so + 0xf74c)
                #24 0x00007f5dda0ebf78 hostfxr_main_startupinfo (libhostfxr.so + 0xbf78)
                #25 0x000055df0a2c927b n/a (/home/luc/work/local/priv/test/AvaloniaApplication1/AvaloniaApplication1/bin/Debug/net7.0/AvaloniaApplication1 + 0xd27b)
                #26 0x000055df0a2c951f n/a (/home/luc/work/local/priv/test/AvaloniaApplication1/AvaloniaApplication1/bin/Debug/net7.0/AvaloniaApplication1 + 0xd51f)
                #27 0x00007f5dd9b4c850 n/a (libc.so.6 + 0x23850)
                #28 0x00007f5dd9b4c90a __libc_start_main (libc.so.6 + 0x2390a)
                #29 0x000055df0a2bf6c5 n/a (/home/luc/work/local/priv/test/AvaloniaApplication1/AvaloniaApplication1/bin/Debug/net7.0/AvaloniaApplication1 + 0x36c5)
                ELF object binary architecture: AMD x86-64

---

#0  Xutf8LookupString (ic=0x0, ev=0x7ffc99255700, buffer=0x55df0d4d9370 "\240S\320\331]\177", nbytes=65536, keysym=0x7ffc99255140, status=0x7ffc99255158) at xlibi18n/ICWrap.c:424

 420     int
 421     Xutf8LookupString(XIC ic, XKeyEvent *ev, char *buffer, int nbytes,
 422                       KeySym *keysym, Status *status)
 423     {
>424         if (ic->core.im) {
 425             if (ic->methods->utf8_lookup_string)
 426                 return (*ic->methods->utf8_lookup_string) (ic, ev, buffer, nbytes,
 427                                                             keysym, status);
 428             else if (ic->methods->mb_lookup_string)
 429                 return (*ic->methods->mb_lookup_string) (ic, ev, buffer, nbytes,
 430                                                             keysym, status);
 431         }
 432         return XLookupNone;
 433     }

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:29 (29 by maintainers)

github_iconTop GitHub Comments

1reaction
iq2luccommented, Jul 6, 2023

OK, thank you for all your time spent on this.

Until it is fixed upstream, I’m going to use the following patch so I can use Avalonia 11 in my projects. Please be so kind and take a (final) quick glance over it and let me know if you think something looks fishy to your trained eye.

diff --git a/src/Avalonia.X11/X11Info.cs b/src/Avalonia.X11/X11Info.cs
index 17cd6b12c..890ac0d5a 100644
--- a/src/Avalonia.X11/X11Info.cs
+++ b/src/Avalonia.X11/X11Info.cs
@@ -34,7 +34,10 @@ internal unsafe class X11Info
         public bool HasXim { get; set; }
         public bool HasXSync { get; set; }
         public IntPtr DefaultFontSet { get; set; }
-        
+
+        [DllImport("libc")]
+        private static extern void setlocale(int type, string s);
+
         public unsafe X11Info(IntPtr display, IntPtr deferredDisplay, bool useXim)
         {
             Display = display;
@@ -48,7 +51,10 @@ public unsafe X11Info(IntPtr display, IntPtr deferredDisplay, bool useXim)
 
             DefaultFontSet = XCreateFontSet(Display, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
                 out var _, out var _, IntPtr.Zero);
-            
+
+            // We have problems with text input otherwise
+            setlocale(0, "");
+
             if (useXim)
             {
                 XSetLocaleModifiers("");
@@ -59,7 +65,15 @@ public unsafe X11Info(IntPtr display, IntPtr deferredDisplay, bool useXim)
 
             if (Xim == IntPtr.Zero)
             {
-                XSetLocaleModifiers("@im=none");
+                if (XSetLocaleModifiers("@im=none") == IntPtr.Zero)
+                {
+                    setlocale(0, "en_US.UTF-8");
+                    if (XSetLocaleModifiers("@im=none") == IntPtr.Zero)
+                    {
+                        setlocale(0, "C.UTF-8");
+                        XSetLocaleModifiers("@im=none");
+                    }
+                }
                 Xim = XOpenIM(display, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
             }
 
diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs
index 12e4e0915..91c0190e8 100644
--- a/src/Avalonia.X11/X11Platform.cs
+++ b/src/Avalonia.X11/X11Platform.cs
@@ -36,12 +36,11 @@ internal class AvaloniaX11Platform : IWindowingPlatform
         public IntPtr OrphanedWindow { get; private set; }
         public X11Globals Globals { get; private set; }
         public ManualRawEventGrouperDispatchQueue EventGrouperDispatchQueue { get; } = new();
-        [DllImport("libc")]
-        private static extern void setlocale(int type, string s);
+
         public void Initialize(X11PlatformOptions options)
         {
             Options = options;
-            
+
             bool useXim = false;
             if (EnableIme(options))
             {
@@ -50,9 +49,6 @@ public void Initialize(X11PlatformOptions options)
                     useXim = true;
             }
 
-            // We have problems with text input otherwise
-            setlocale(0, "");
-
             XInitThreads();
             Display = XOpenDisplay(IntPtr.Zero);
             if (Display == IntPtr.Zero)
@@ -64,7 +60,7 @@ public void Initialize(X11PlatformOptions options)
             OrphanedWindow = XCreateSimpleWindow(Display, XDefaultRootWindow(Display), 0, 0, 1, 1, 0, IntPtr.Zero,
                 IntPtr.Zero);
             XError.Init();
-            
+
             Info = new X11Info(Display, DeferredDisplay, useXim);
             Globals = new X11Globals(this);
             //TODO: log
diff --git a/src/Avalonia.X11/X11Window.Ime.cs b/src/Avalonia.X11/X11Window.Ime.cs
index 4ee418fd0..1bc66379b 100644
--- a/src/Avalonia.X11/X11Window.Ime.cs
+++ b/src/Avalonia.X11/X11Window.Ime.cs
@@ -133,9 +133,17 @@ private unsafe string TranslateEventToString(ref XEvent ev)
         {
             if (ImeBuffer == IntPtr.Zero)
                 ImeBuffer = Marshal.AllocHGlobal(ImeBufferSize);
-            
-            var len = Xutf8LookupString(_xic, ref ev, ImeBuffer.ToPointer(), ImeBufferSize, 
-                out _, out var istatus);
+
+            IntPtr istatus;
+            int len;
+            if(_xic != IntPtr.Zero) {
+                len = Xutf8LookupString(_xic, ref ev, ImeBuffer.ToPointer(),
+                    ImeBufferSize, out _, out istatus);
+            }
+            else {
+                len = XLookupString(ref ev, ImeBuffer.ToPointer(), ImeBufferSize,
+                    out _, out istatus);
+            }
             var status = (XLookupStatus)istatus;
 
             if (len == 0)
diff --git a/src/Avalonia.X11/XLib.cs b/src/Avalonia.X11/XLib.cs
index 641adde7e..d129e4b02 100644
--- a/src/Avalonia.X11/XLib.cs
+++ b/src/Avalonia.X11/XLib.cs
@@ -468,7 +468,7 @@ public enum XLookupStatus : uint
         public static extern unsafe int XLookupString(ref XEvent xevent, void* buffer, int num_bytes, out IntPtr keysym, out IntPtr status);
         
         [DllImport (libX11)]
-        public static extern unsafe int Xutf8LookupString(IntPtr xic, ref XEvent xevent, void* buffer, int num_bytes, out IntPtr keysym, out UIntPtr status);
+        public static extern unsafe int Xutf8LookupString(IntPtr xic, ref XEvent xevent, void* buffer, int num_bytes, out IntPtr keysym, out IntPtr status);
         
         [DllImport (libX11)]
         public static extern unsafe int Xutf8LookupString(IntPtr xic, XEvent* xevent, void* buffer, int num_bytes, out IntPtr keysym, out IntPtr status);

I tested it on all my 5 computers, and everything works as expected: no more segfaults and UTF-8 multi-byte chars are properly translated.

Thanks again, Luc

0reactions
iq2luccommented, Jul 24, 2023

Done, see https://github.com/AvaloniaUI/Avalonia/pull/12313. I hope it is usable on your side with little effort, I changed it a bit by trying to match Avalonia’s coding style (which - sadly - doesn’t place single-line if statements inside braces).

Thank you again for all your support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TextBox
The TextBox control is an editable text field where a user can input text.
Read more >
Newest 'avaloniaui' Questions - Page 6
I'm trying out styles in avalonia and most works, except for pseudoclasses, they just get ignored. I have created a window and all...
Read more >
Overriding Sealed Methods in C# - InfoQ
In this article, the author demonstrates how we can change the behavior of sealed methods in C#. This can be done by understanding...
Read more >
Newest 'visual-studio' Questions - Page 265
Lets say I have classes like the following: public class Input<T> { // some data here but type T is never directly used...
Read more >
开源项目 - 林德熙
XamlFlair/XamlFlair: XamlFlair is an animation library for UWP, WPF, and Uno, built to facilitate Xaml animations using only attached properties. WPFTOOLS.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found