Tk.jl icon indicating copy to clipboard operation
Tk.jl copied to clipboard

InexactError calling TkWinReleaseDrawableDC

Open ncnc opened this issue 10 years ago • 5 comments

I'm trying to use Winston with latest master and I'm getting very frequent InexactErrors when calling plot, apparently due to the ccall to TkWinReleaseDrawableDC in tkwidget.jl.

The below patch doesn't fix the root cause, but makes plotting work:

diff --git a/src/tkwidget.jl b/src/tkwidget.jl
index a226e77..5f7dd93 100644
--- a/src/tkwidget.jl
+++ b/src/tkwidget.jl
@@ -373,8 +373,13 @@ function render_to_cairo(f::Function, w::TkWidget, clipped::Bool=true)
         surf = CairoWin32Surface(hdc, width(w), height(w))
         f(surf)
         destroy(surf)
-        ccall((:TkWinReleaseDrawableDC,libtk), Void, (Int, Int, Ptr{Uint8}),
-            drawable, hdc, state)
+        try
+            ccall((:TkWinReleaseDrawableDC,libtk), Void,
+              (Int, Int, Ptr{Uint8}), drawable, hdc, state)
+        catch e
+            println("Error during TkWinReleaseDrawableDC: ")
+            Base.display_error(e,catch_backtrace())
+        end
         return
     end
     error("Unsupported Operating System")

The error looks like this (relative to the patched code, sorry):

Error during TkWinReleaseDrawableDC:
ERROR: InexactError()
 in render_to_cairo at C:\Users\ncnc\.julia\v0.4\Tk\src\tkwidget.jl:377
 in anonymous at C:\Users\ncnc\.julia\v0.4\Tk\src\tkwidget.jl:395
 in jl_tcl_callback at C:\Users\ncnc\.julia\v0.4\Tk\src\tkwidget.jl:145

I'm using a fresh 32-bit windows build, but I noticed the same problem with the latest official nightly build.

julia> versioninfo()
Julia Version 0.4.0-dev+2269
Commit 65a0959 (2014-12-26 06:25 UTC)
Platform Info:
  System: Windows (i686-w64-mingw32)
  CPU: Intel(R) Core(TM)2 CPU          4300  @ 1.80GHz
  WORD_SIZE: 32
  BLAS: libopenblas (DYNAMIC_ARCH NO_AFFINITY Core2)
  LAPACK: libopenblas
  LIBM: libopenlibm
  LLVM: libLLVM-3.3

ncnc avatar Dec 26 '14 17:12 ncnc

Should we maybe be using a different integer size for TkWinReleaseDrawableDC ?

tkelman avatar Jan 02 '15 04:01 tkelman

@ncnc, you could insert

@show drawable
@show hdc

before that ccall to inspect their values. You could try changing those Int to Uint.

http://fossies.org/dox/brx.src-1.6.0/tkWinDraw_8c.html#a3f9d9081db7647fc6cf744b66f1064db seems to suggest those should be unsigned long. I'm not exactly sure how big that is on different Windows platforms.

timholy avatar Jan 09 '15 09:01 timholy

I'm not exactly sure how big that is on different Windows platforms.

This is what the Culong type is for. It's UInt32 on win64

tkelman avatar Jan 09 '15 13:01 tkelman

Thanks. I did indeed see unsigned 32-bit values when using @show.

julia> using Winston

julia> plot(0:01:1,sin((0:0.01:1)*4))
drawable = 185896600
hdc = Ptr{Void} @0x01010b3a
drawable = drawable = 185896600
hdc = Ptr{Void} @0x10010a41
185896600
hdc = Ptr{Void} @0xff010760
Error during TkWinReleaseDrawableDC:

Note the 0xff010760 before the exception. So at least hdc has an unsigned 32-bit value before the crash. Changing the second argument to Culong as suggested by @tkelman makes everything work smoothly in my end.

diff --git a/src/tkwidget.jl b/src/tkwidget.jl
index a226e77..9c75768 100644
--- a/src/tkwidget.jl
+++ b/src/tkwidget.jl
@@ -373,7 +373,7 @@ function render_to_cairo(f::Function, w::TkWidget, clipped::Bool=true)
         surf = CairoWin32Surface(hdc, width(w), height(w))
         f(surf)
         destroy(surf)
-        ccall((:TkWinReleaseDrawableDC,libtk), Void, (Int, Int, Ptr{Uint8}),
+        ccall((:TkWinReleaseDrawableDC,libtk), Void, (Int, Culong, Ptr{Uint8}),
             drawable, hdc, state)
         return
     end

According to the documents linked by @timholy, drawable is defined as an unsigned long too. On the other hand, it seems like the current Int is used in multiple places to no ill effect, so maybe that bit isn't really used by Tk after all.

ncnc avatar Jan 09 '15 21:01 ncnc

Thinking aloud here. It seems like julia interprets the hdc is a pointer, so maybe hdc should be a pointer type...

ncnc avatar Jan 09 '15 21:01 ncnc