# GDK BadAccess error when opening window for first time inside docker

**URL:** <https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109>\
**Category:** Python\
**Tags:** highgui, docker\
**Created:** [November 25, 2022, 3:51pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109 "2022-11-25T15:51:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartonp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bartonp/32/1396_2.png) [@bartonp](https://forum.opencv.org/u/bartonp)\
**Post date:** [November 25, 2022, 3:51pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/1 "2022-11-25T15:51:33Z")

</div>

I have a docker image with opencv installed via apt. When I run my program it crashes when opening an opencv window with `cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)` resulting in the following error:

```auto
(python3:218): Gdk-ERROR **: 15:41:23.282: The program 'python3' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
  (Details: serial 287 error_code 10 request_code 130 (MIT-SHM) minor_code 1)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the GDK_SYNCHRONIZE environment
   variable to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)

```

When I run the program again it works without problems. However, every time I start the docker new again I get this error on the first try. Further tries usually work without problems.

Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [November 26, 2022, 7:26am UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/2 "2022-11-26T07:26:41Z")

</div>

It could be that during the first run, Docker hadn’t initialized its environment before your window call - of course it probably should have.

I would try adding a delay of couple of seconds in the beginning of the program to give Docker time to settle

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 26, 2022, 11:04am UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/3 "2022-11-26T11:04:24Z")

</div>

docker + GUI/graphical issue.

try replicating this with the “xclock” program. if that also fails in this manner, you’ve got a reduced [MRE](https://stackoverflow.com/help/minimal-reproducible-example) that no longer needs OpenCV.

---

<div class="post-metadata">

**Author:** ![bartonp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bartonp/32/1396_2.png) [@bartonp](https://forum.opencv.org/u/bartonp)\
**Post date:** [November 28, 2022, 2:41pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/4 "2022-11-28T14:41:42Z")

</div>

This didn’t solve the problem, but it has lead me to realise the error is not necessarily originating from the `cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)` command. Rather, the error appears only when the image is shown with: `cv2.waitKey(100)`.

So if this is not an opencv problem, where should I ask about this?

---

<div class="post-metadata">

**Author:** ![bartonp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bartonp/32/1396_2.png) [@bartonp](https://forum.opencv.org/u/bartonp)\
**Post date:** [November 28, 2022, 2:44pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/5 "2022-11-28T14:44:55Z")

</div>

xclock works straight away without problems

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 28, 2022, 11:41pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/6 "2022-11-28T23:41:06Z")

</div>

> [@bartonp](#):
>
> `cv2.waitKey(100)`.

that just spins the gui event loop. that probably uses GTK. see if you can run any GTK-using programs in your container.

xclock, afaik, talks directly to the X server. if that works, we know _that_ part of the graphics stack works at least.

---

<div class="post-metadata">

**Author:** ![bartonp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bartonp/32/1396_2.png) [@bartonp](https://forum.opencv.org/u/bartonp)\
**Post date:** [November 29, 2022, 12:04pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/7 "2022-11-29T12:04:26Z")

</div>

Alright, so I installed gedit which uses GTK and it has the same problem. It crashes the first time it is run with the GDK BadAccess Error. So I guess the problem lies with the GDK installation and docker.

---

<div class="post-metadata">

**Author:** ![bartonp](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/bartonp/32/1396_2.png) [@bartonp](https://forum.opencv.org/u/bartonp)\
**Post date:** [November 30, 2022, 12:40pm UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/8 "2022-11-30T12:40:20Z")

</div>

So I managed to solve the issue for anyone else reading this thread. The solution was to add the `--ipc host` flag when running the docker image as described here: [attempt to access private resource denied · Issue #21 · osrf/docker\_images · GitHub](https://github.com/osrf/docker_images/issues/21)

With this it worked on the first try

---

<div class="post-metadata">

**Author:** ![diviner216](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diviner216/32/9939_2.png) [@diviner216](https://forum.opencv.org/u/diviner216)\
**Post date:** [March 16, 2024, 6:53am UTC](https://forum.opencv.org/t/gdk-badaccess-error-when-opening-window-for-first-time-inside-docker/11109/9 "2024-03-16T06:53:07Z")

</div>

love you my friend , you really help me solve a problem that confusing me for days
