smasherprog / screen_capture_lite

cross platform screen/window capturing library
MIT License
616 stars 156 forks source link

macOS segfault when resizing #132

Open kisasexypantera94 opened 2 years ago

kisasexypantera94 commented 2 years ago

I am trying to capture window output on macOS:

#include <iostream>
#include <locale>

#include "ScreenCapture.h"

using namespace std;

int main()
{
  auto windowframgrabber =
      SL::Screen_Capture::CreateCaptureConfiguration(
          []()
          {
            try
            {
              auto windows = SL::Screen_Capture::GetWindows();
              return windows;
            }
            catch (const std::exception& e)
            {
              cout << "ERROR: " << e.what() << endl;
              return std::vector<SL::Screen_Capture::Window>{};
            }

          })
          ->onFrameChanged(
              [&](const SL::Screen_Capture::Image& img, const SL::Screen_Capture::Window& window) {
                // cout << "FrameChanged: " << window.Name << endl;
              })
          ->onNewFrame(
              [&](const SL::Screen_Capture::Image& img, const SL::Screen_Capture::Window& window) {
                // cout << "NewFrame: " << window.Name << endl;
              })
          ->onMouseChanged(
              [&](const SL::Screen_Capture::Image* img,
                  const SL::Screen_Capture::MousePoint& mousepoint) {
              })
          ->start_capturing();

  windowframgrabber->setFrameChangeInterval(std::chrono::milliseconds(100));  // 100 ms
  windowframgrabber->setMouseChangeInterval(std::chrono::milliseconds(100));  // 100 ms

  while (true)
  {

  }
}

However when I am dragging windows (three finger up gesture) it crashes with segfault.

I tried debugging but with lldb it immediately crashes even without dragging:

➜  build git:(master) ✗ sudo lldb screenshare_static
Password:
(lldb) target create "screenshare_static"
Current executable set to '/Users/a19531364/dev/screen_capture_lite/build/screenshare_static' (x86_64).
(lldb) run
error: shell expansion failed (reason: lldb-argdumper exited with error 1). consider launching with 'process launch'.
(lldb) process launch
Process 74255 launched: '/Users/a19531364/dev/screen_capture_lite/build/screenshare_static' (x86_64)
Process 74255 stopped
* thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x00007fff203960f5 CoreFoundation`CFStringGetCString + 57
CoreFoundation`CFStringGetCString:
->  0x7fff203960f5 <+57>: movq   (%rbx), %rax
    0x7fff203960f8 <+60>: testq  %rax, %rax
    0x7fff203960fb <+63>: je     0x7fff20396128            ; <+108>
    0x7fff203960fd <+65>: leaq   0x5fcc4a84(%rip), %rcx    ; __CFConstantStringClassReferencePtr
Target 0: (screenshare_static) stopped.
(lldb) up
screenshare_static was compiled with optimization - stepping may behave oddly; variables may not be available.
frame #1: 0x000000010000d71c screenshare_static`SL::Screen_Capture::GetWindows() at GetWindows.cpp:55:13 [opt]
   52               uint32_t windowid=0;
   53               auto dict = static_cast<CFDictionaryRef>(CFArrayGetValueAtIndex(windowList, i));
   54               auto cfwindowname = static_cast<CFStringRef>(CFDictionaryGetValue(dict, kCGWindowName));
-> 55               CFStringGetCString(cfwindowname, w.Name, sizeof(w.Name), kCFStringEncodingUTF8);
   56               w.Name[sizeof(w.Name)-1] = '\n';
   57
   58
(lldb)
kisasexypantera94 commented 2 years ago

It fixed the problem:

auto cfwindowname = static_cast<CFStringRef>(CFDictionaryGetValue(dict, kCGWindowName));
if (not cfwindowname)
{
  continue;
}

Could this be the solution?

smasherprog commented 2 years ago

If a window gets closed the application should handle this gracefully. do a PR on it!