From d49109877c887416f146a09d9c83d0bbae3e4abe Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 20 Jan 2020 11:47:04 -0300 Subject: [PATCH 1/5] X11 backend wont crash with 0 sized window. --- .../PlatformSupport/StandardRuntimePlatform.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs index 8a5a7255944..499f263a9af 100644 --- a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs +++ b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs @@ -52,11 +52,13 @@ static UnmanagedBlob() public UnmanagedBlob(StandardRuntimePlatform plat, int size) { - if (size <= 0) - throw new ArgumentException("Positive number required", nameof(size)); - _plat = plat; - _address = plat.Alloc(size); - GC.AddMemoryPressure(size); + if (size > 0) + { + _plat = plat; + _address = plat.Alloc(size); + GC.AddMemoryPressure(size); + } + Size = size; #if DEBUG _backtrace = Environment.StackTrace; @@ -75,7 +77,7 @@ void DoDispose() lock (_btlock) Backtraces.Remove(_backtrace); #endif - _plat.Free(_address, Size); + _plat?.Free(_address, Size); GC.RemoveMemoryPressure(Size); IsDisposed = true; _address = IntPtr.Zero; From 9fc7842eefcce5ad24300f27eb76f4eff7efa4b4 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 20 Jan 2020 11:52:33 -0300 Subject: [PATCH 2/5] Revert "X11 backend wont crash with 0 sized window." This reverts commit d49109877c887416f146a09d9c83d0bbae3e4abe. --- .../PlatformSupport/StandardRuntimePlatform.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs index 499f263a9af..8a5a7255944 100644 --- a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs +++ b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs @@ -52,13 +52,11 @@ static UnmanagedBlob() public UnmanagedBlob(StandardRuntimePlatform plat, int size) { - if (size > 0) - { - _plat = plat; - _address = plat.Alloc(size); - GC.AddMemoryPressure(size); - } - + if (size <= 0) + throw new ArgumentException("Positive number required", nameof(size)); + _plat = plat; + _address = plat.Alloc(size); + GC.AddMemoryPressure(size); Size = size; #if DEBUG _backtrace = Environment.StackTrace; @@ -77,7 +75,7 @@ void DoDispose() lock (_btlock) Backtraces.Remove(_backtrace); #endif - _plat?.Free(_address, Size); + _plat.Free(_address, Size); GC.RemoveMemoryPressure(Size); IsDisposed = true; _address = IntPtr.Zero; From 1f1d3a8e35ccc02889a28477ce1e3320d4146707 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 20 Jan 2020 11:53:26 -0300 Subject: [PATCH 3/5] prevent crashing in finalizer --- src/Shared/PlatformSupport/StandardRuntimePlatform.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs index 8a5a7255944..a23c31ca644 100644 --- a/src/Shared/PlatformSupport/StandardRuntimePlatform.cs +++ b/src/Shared/PlatformSupport/StandardRuntimePlatform.cs @@ -75,7 +75,7 @@ void DoDispose() lock (_btlock) Backtraces.Remove(_backtrace); #endif - _plat.Free(_address, Size); + _plat?.Free(_address, Size); GC.RemoveMemoryPressure(Size); IsDisposed = true; _address = IntPtr.Zero; From 32df775398682e31ad2dafc9ccb95836e3622ad3 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 20 Jan 2020 12:06:31 -0300 Subject: [PATCH 4/5] Add hack to prevent renderer crashing on 0x0 window x11. --- src/Avalonia.X11/X11Framebuffer.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Avalonia.X11/X11Framebuffer.cs b/src/Avalonia.X11/X11Framebuffer.cs index 00288f300d4..94f930e9ec9 100644 --- a/src/Avalonia.X11/X11Framebuffer.cs +++ b/src/Avalonia.X11/X11Framebuffer.cs @@ -14,6 +14,10 @@ class X11Framebuffer : ILockedFramebuffer public X11Framebuffer(IntPtr display, IntPtr xid, int depth, int width, int height, double factor) { + // HACK! Please fix renderer, should never ask for 0x0 bitmap. + width = Math.Max(1, width); + height = Math.Max(1, height); + _display = display; _xid = xid; _depth = depth; From 7e42523008752f4c86b8b8f5bcb995168d7ec077 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Mon, 20 Jan 2020 23:27:00 +0800 Subject: [PATCH 5/5] Additional sanity checks for monitor resolution --- src/Avalonia.X11/X11Window.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index 6ff5b96f12a..919abae2437 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -102,7 +102,7 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent) valueMask |= SetWindowValuemask.ColorMap; } - int defaultWidth = 300, defaultHeight = 200; + int defaultWidth = 0, defaultHeight = 0; if (!_popup && Screen != null) { @@ -117,6 +117,10 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent) } } + // check if the calculated size is zero then compensate to hardcoded resolution + defaultWidth = Math.Max(defaultWidth, 300); + defaultHeight = Math.Max(defaultHeight, 200); + _handle = XCreateWindow(_x11.Display, _x11.RootWindow, 10, 10, defaultWidth, defaultHeight, 0, depth, (int)CreateWindowArgs.InputOutput, @@ -133,7 +137,7 @@ public X11Window(AvaloniaX11Platform platform, IWindowImpl popupParent) _renderHandle = _handle; Handle = new PlatformHandle(_handle, "XID"); - _realSize = new PixelSize(300, 200); + _realSize = new PixelSize(defaultWidth, defaultHeight); platform.Windows[_handle] = OnEvent; XEventMask ignoredMask = XEventMask.SubstructureRedirectMask | XEventMask.ResizeRedirectMask