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.

AttributeError: _data / "gdi32.GetDIBits() failed" (Windows)

See original GitHub issue

General information:

  • OS name: Windows
  • OS version: 10, version 1803
  • OS architecture: 64-bit
  • Resolutions:
    • Monitor 1: 1920x1080
    • Monitor 2: 1920x1080
  • Python version: 3.6.5
  • MSS version: 3.2.1

Description of the warning/error

MSS consistently throws the following error, always after my program has been running for a little over 3 hours, and after successfully capturing many (22,000+) screenshots in that time.

Full message

== 2018-07-15 07:17:17.953381 runtime: 3:09:39.428722 ==
('gdi32.GetDIBits() failed.', {'bits': 0, 'height': 665, 'width': 1033, 'gdi': <WinDLL 'gdi32', handle 7ffeff9b0000 at 0x1e9c1896898>, 'monitor': {'left': -1034, 'top': 31, 'width': 1033, 'height': 665}, 'self': <mss.windows.MSS object at 0x000001E9C1896860>})
Traceback (most recent call last):
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 98, in <module>
    run()
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 82, in run
    self.screenshot()
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 77, in screenshot
    self.im = self.window.screenshot()
  File "D:\IdeaProjects\Python\CaptureTest\main.py", line 84, in screenshot
    im = self.mss_instance.grab(area)
  File "C:\Users\Nick\AppData\Local\Programs\Python\Python36\lib\site-packages\mss\windows.py", line 202, in grab
    del self._data
AttributeError: _data

Other details

I’ve tried to replicate this error by simply calling mss.grab(...) in a while loop and seeing if it breaks after x number of screenshots, but that runs fine for at least 160,000 iterations, so I can’t figure out why my program consistently seems to break at ~3 hours runtime.

Upon viewing the MSS source, I realised that the del self._data line is directly followed by raise ScreenShotError('gdi32.GetDIBits() failed.', locals()), so I suppose my question is:

What could cause gdi32.GetDIBits() to spontaneously fail?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
BoboTiGcommented, Jul 16, 2018

I think I found the cause.

Redefine Window.exists() to free the allocated DC:

def exists(self):
    try:
        dc = win32gui.GetWindowDC(self.hwnd)
    except:
        return False
    else:
        win32gui.DeleteObject(dc)
        return True

Another benefit: this will be much faster.

0reactions
BoboTiGcommented, Feb 24, 2019

OK, long time since this issue was resolved, but i had time to check for a useful error message. This is was I found: GDI functions do not use LastError(), only return 0 on error.

I followed how ReactOS is doing and this is the revelant parts:

So, there is no way to get some error message …

Doing this investgation could open a new idea to improve speed on Windows: directly calls GreGetDIBitsInternal, if available.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError: _data / "gdi32.GetDIBits() failed" (Windows)
General information: OS name: Windows; OS version: 10, version 1803; OS architecture: 64-bit; Resolutions:.
Read more >
GetDIBits failing when trying to save screenshot with ctypes?
I'm attempting to take and save a screenshot using just ctypes . I'm not terrifically familiar with the Windows API, so I've been...
Read more >
GetDIBits function (wingdi.h) - Win32 apps - Microsoft Learn
The GetDIBits function retrieves the bits of the specified compatible bitmap and copies them into a buffer as a DIB using the specified ......
Read more >
Viewing online file analysis results for 'Setup.exe'
Writes data to a remote process; Fingerprint: Queries kernel debugger information. Queries process information. Queries sensitive IE security settings
Read more >
Use takerFree in fMBT With Examples | LambdaTest
... AttributeError: raise NotImplementedError("this windows version does not ... CloseClipboard() raise Exception("error in allocating global memory for ...
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