Skip to content

Commit

Permalink
fix close/reopen crash on linux. Fixes #249.
Browse files Browse the repository at this point in the history
cairo_device_finish must be called before closing the xcb connection,
otherwise the invalid connection will not be removed from an internal
cairo cache. When reopening the editor there's then a risk that the
new connection has the same pointer value as the old one and that will
cause a crash.

The call to cairo_device_finish should not be made until the last
instance of the editor has been closed. If it's done earlier, the
remaining editors will stop working.
  • Loading branch information
andreas56 committed Jun 6, 2022
1 parent 63fb675 commit 590b732
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions vstgui/lib/platform/linux/x11frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ struct DrawHandler

~DrawHandler ()
{
RunLoop::instance ().setDevice (device);
cairo_device_destroy (device);
}

Expand Down
15 changes: 15 additions & 0 deletions vstgui/lib/platform/linux/x11platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ struct RunLoop::Impl : IEventHandler
std::array<xcb_cursor_t, CCursorType::kCursorIBeam + 1> cursors {{XCB_CURSOR_NONE}};
KeyboardEvent lastUnprocessedKeyEvent;
uint32_t lastUtf32KeyEventChar {0};
cairo_device_t* device {nullptr};

void init (const SharedPointer<IRunLoop>& inRunLoop)
{
Expand Down Expand Up @@ -166,6 +167,11 @@ struct RunLoop::Impl : IEventHandler
{
if (--useCount != 0)
return;

cairo_device_finish (device);
cairo_device_destroy (device);
device = nullptr;

if (xcbConnection)
{
if (xkbUnprocessedState)
Expand Down Expand Up @@ -550,6 +556,15 @@ Optional<UTF8String> RunLoop::convertCurrentKeyEventToText () const
return {};
}

void RunLoop::setDevice (cairo_device_t* device)
{
if (impl->device != device)
{
cairo_device_destroy (impl->device);
impl->device = cairo_device_reference (device);
}
}

//------------------------------------------------------------------------
} // X11
} // VSTGUI
2 changes: 2 additions & 0 deletions vstgui/lib/platform/linux/x11platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "x11frame.h"
#include <atomic>
#include <memory>
#include <cairo/cairo.h>

struct xcb_connection_t; // forward declaration
struct xcb_key_press_event_t; // forward declaration
Expand Down Expand Up @@ -62,6 +63,7 @@ struct RunLoop
KeyboardEvent&& getCurrentKeyEvent () const;
Optional<UTF8String> convertCurrentKeyEventToText () const;

void setDevice (cairo_device_t* device);
static RunLoop& instance ();

private:
Expand Down

0 comments on commit 590b732

Please sign in to comment.