Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FigureCanvasTkAgg - "buffer is of wrong type" error during blit #9799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
eythimis opened this issue Nov 16, 2017 · 7 comments
Closed

FigureCanvasTkAgg - "buffer is of wrong type" error during blit #9799

eythimis opened this issue Nov 16, 2017 · 7 comments

Comments

@eythimis
Copy link

Bug report

Bug summary

I am trying to embed matplotlib in a Tk Canvas using FigureCanvasTkAgg. The Tk canvas itself is embedded in a Tk Frame, that can be resized. At certain dimensions of the Tk Frame the FigureCanvasTkAgg fails during the call to its draw function and throws the following exception:

File "...\python3.4\win64d\lib\site-packages\matplotlib-1.5.3.egg\matplotlib\backends\tkagg.py", line 30, in blit
    id(data), colormode, id(bbox_array))
tkinter.TclError: buffer is of wrong type

I am using this functionality in a greater cae standalone program that uses Tk for its gui and the problem only happens when the cae files that we load on our program occupy around 2gb of RAM. When smaller files are loaded this issue does not appear. However if before loading the small files, we have loaded large files the issue keeps coming up even for the small ones.

I used the code from matplotlib site (https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_tk.html) to demonstrate that the issue does not have to do with my code, since when the large models are loaded, this example also produces the same errors.
Now, after this error appears, if the user resizes the window and is lucky to find a correct dimension for the Tk window that contains the canvas, then the issue stops appearing and the FigureCanvasTkAgg draws itself successfully.

I understand that the exception is raised from this code https://github.com/matplotlib/matplotlib/blob/v1.5.3/lib/matplotlib/backends/tkagg.py (line 32)
and by searching a little bit I have found that the call to PyAggImagePhoto fails, probably in this code https://github.com/matplotlib/matplotlib/blob/v1.5.3/src/_tkagg.cpp (line 85).
If I am not mistaken, it tries to instantiate a numpy::array_view class passing it the figureCanvasTkAgg.renderer._renderer python object as a parameter, and somehow this throws the exception.

I have even tested with this code https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_tk_canvas.html (it is using FigureCanvasAgg) and the issue still appears.

Code for reproduction

You can use the example from matplotlib site (https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_tk.html)

# My code is a little bit different from the sites's
# since I am using a personal popupdialog that I have created to make use in
#our program, but the concept is the same

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

f = mpl.figure.Figure(figsize=(4, 3), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2*pi*t)

a.plot(t, s)

graphGui = PopUpDialog() 
canvas = FigureCanvasTkAgg(f, master=graphGui.body)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
The canvas is not drawn and the following error appears

File "...\python3.4\win64d\lib\site-packages\matplotlib-1.5.3.egg\matplotlib\backends\tkagg.py", line 30, in blit
    id(data), colormode, id(bbox_array))
tkinter.TclError: buffer is of wrong type

Expected outcome
The canvas should be drawn and the error should not appear

Matplotlib version

  • Operating system: Windows 10 Pro 64-bit, 32 GB RAM,
  • Graphics: NVIDIA QUADRO M2200, Resolution 2560 x 1440
  • Matplotlib version: 1.5.3
  • Matplotlib backend : TkAgg
  • Python version: 3.4.3 (default, Feb 26 2015, 07:46:55) [MSC v.1600 64 bit (AMD64)]
  • Jupyter version (if applicable):
  • Other libraries: numpy 1.11.2, but also tested with numpy 1.13.3 and the same problem appears

Matplotlib has been installed through pip.

@dopplershift
Copy link
Contributor

Can you update your matplotlib to 2.1? Those are working for me on osx--though the embedding_in_tk_sgskip.py has some pretty nasty flickering.

@eythimis
Copy link
Author

Unfortunately, still the same issue

@eythimis eythimis reopened this Nov 17, 2017
@eythimis
Copy link
Author

eythimis commented Nov 17, 2017

I have found something that might help,
When the canvas is not drawn on my Tk canvas, I tried to output it in a png image using the following command

FigureCanvasAgg.print_png(myCanvasObj,"c:/temp/test.png")

and the image was correctly written to the png file.

Hence the data are there but cannot be plot in Tk canvas??

@tacaswell
Copy link
Member

Is that the full traceback you got?

Can you get any debugging hooks in to sort out exactly what is in bufferobj when this goes wrong? It looks like this is an exception we produce

numpy::array_view<uint8_t, 3> buffer;
try {
buffer = numpy::array_view<uint8_t, 3>(bufferobj);
} catch (...) {
TCL_APPEND_RESULT(interp, "buffer is of wrong type", (char *)NULL);
PyErr_Clear();
return TCL_ERROR;
}
but we are probably being too blunt in the way we are catching all exceptions there.

When you are having these issues, how much free memory is there on the system?

Where does PopUpDialog() come from?

@tacaswell tacaswell added this to the v2.2 milestone Nov 20, 2017
@eythimis
Copy link
Author

eythimis commented Nov 20, 2017

Yes unfortunately that is the full traceback I have got.

I believe (as seen from the code) that the bufferobj is argv[2] and from https://github.com/matplotlib/matplotlib/blob/v1.5.3/lib/matplotlib/backends/tkagg.py I can assume that the argument that is passed in position 2 is the memory position- id(data)- from the data = np.asarray(aggimage) as seen in https://github.com/matplotlib/matplotlib/blob/v1.5.3/lib/matplotlib/backends/tkagg.py.
This aggimage is the _RendererAgg as taken from FigureCanvasTkAgg.renderer._renderer and converted to numpyarray. using np.asarray(...).

From a windbg session I have found that there is an ACCESS VIOLATION crash in numpy's multiarray

(numpy-1.11.2-py3.4-win-amd64.egg\numpy\core\multiarray.pyd )
Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
multiarray+0x457a0:
00007ffca27057a0 488b4908 mov rcx,qword ptr [rcx+8] ds:0000000000000007=????????????????

StackTrace

multiarray + 0x457a0
multiarray + 0x45d7a
_tkagg!PyInit__tkagg + 0x652
_tkagg!PyInit__tkagg + 0x189
tcl85tg!TclInvokeStringCommand + 0xc7
tcl85tg!TclEvalObjvInternal + 0x6b4
tcl85tg!Tcl_EvalObjv + 0xa0

I can assume that probably import_array (https://github.com/matplotlib/matplotlib/blob/v1.5.3/src/_tkagg.cpp, line 456) might causing some load failure, as I see it in a lot of github issues (for example, https://stackoverflow.com/questions/12879226/error-occurs-when-i-write-my-own-c-extension-for-numpy)

Another thing though is that, when I get this exception and call FigureCanvasTkAgg.draw() again and again, it finally succeeds...

I have 32Gb of RAM and at the time that this error appears I have plenty of RAM available. The problem is if there is some Tkinter limitation on memory per application - which I don't know - that causes these issues, but surely a better explanation on the exception would clear things out. Do you believe it would be easy to send me a compiled version of Matplotlib which would explain the exception better?

PopUpDialog is an internal class of our program which is basically a Tk popupwindow with a 2 Tk Frames. The top Tk Frame is used to place FiguraCanvasTkAgg inside.

@anntzer
Copy link
Contributor

anntzer commented Dec 15, 2017

Can you check whether #9356 fixes this issue?

@eythimis
Copy link
Author

eythimis commented Oct 8, 2018

Matplotlib 3.0.0 resolves this issue

@eythimis eythimis closed this as completed Oct 8, 2018
@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants