cdhowie's picture

Crash in GraphicsMode.get_Default when creating GLWidget

I'm attempting to build a simple GLWidget project so that I can experiment with some node layout algorithms, and I get the following right out of the gate:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at OpenTK.Graphics.GraphicsMode.get_Default () [0x00000] in <filename unknown>:0
  at Gtk.GLWidget..ctor () [0x00000] in <filename unknown>:0

I am running inside of an Xvnc server. glxinfo reports quite a bit of information, and glxgears seems to work just fine, so I doubt it's the GLX extension that's failing somewhere.

So basically... is this a bug?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cdhowie's picture

It seems that this stems from the fact that Xvnc does not implement either Xinerama or XRandR. Is there a way to work around this in X11DisplayDevice? I find it rather odd that either Xinerama or XRandR would be required at all. I'd expect at least a fallback on another method that is guaranteed to exist in every X server.

the Fiddler's picture

The exception originates in GraphicsMode.get_Default which doesn't use Xinerama/XRandR. Can you please compile a debug version from SVN ( and test with that? The stacktrace and debug output should reveal more information on this error.

(OpenTK should work without Xinerama (1999) and XRandR (2001) anyway. It is possible to implement a XF86 fallback (1991, see X11DisplayDevice.cs:201) but I won't be doing that any time soon.)

cdhowie's picture

Sorry, I should have given more info. I did compile my own version and tinkered (quite a bit actually) with the sources. I don't have the code in front of me at the moment, but I can tell you the basic sequence of events.

GLWidget:.ctor() calls GraphicsMode.get_Default, which calls DisplayDevice.get_Default, which is null because the X11 DisplayDevice factory (I forget exactly what it's called...) doesn't find any devices, in turn because Xvnc has no XRandR or Xinerama. Since GraphicsMode.get_Default does not check the return value from DisplayDevice.get_Default, the NRE is thrown.

the Fiddler's picture

Ah, yes, you are right.

I'll modify X11DisplayDevice to set some sensible defaults when it Xinerama/XRandR aren't available. Chances are xvnc doesn't support XF86 either, so that's the only solution that would allow it to work.

cdhowie's picture

Sounds good. I know that GLX can provide info on what color depths and bits-per-rgb are available. And in Xvnc, the current video mode may not necessarily represent available GLX modes.

For example, even when running Xvnc with -depth 24, there is no GLX mode with a depth of 24. 16 is the highest depth supported, so even if it did support the XF86 call you might be left with data that cannot be used to create a working GL context anyway.

carga's picture

I'm expecting exactly the same NRE in slightly different situation. I downloaded OpenTK v1.0 (named October, 6 2010) and unzipped it. I am trying to start Example.exe to see your amazing samples.

The demo shell works, but when I try to call any sample (Julia Set, or any other) I receive the same error.

Then I extracted GameWindowsSimple.cs in separate solution -- the same issue with stack trace like this:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
  at OpenTK.Graphics.GraphicsMode.get_Default () [0x00000] 
  at OpenTK.GameWindow..ctor (Int32 width, Int32 height) [0x00000] 
  at Examples.Tutorial.SimpleWindowFps..ctor () [0x00000] 
  at Examples.Tutorial.SimpleWindowFps.Main () [0x00000]

My environment is Ubuntu 10.04 64bit, some nVidia GPU (I just don't remember it's model). glxgears works fine.

I have dual monitor configuration, but it is NOT Xinerama. Just two completely separate workspaces. Is it the root of the problem? How should I overcome the issue?

Have a fast code!

the Fiddler's picture

Can you please test with SVN trunk? It contains several fixes for non-xrandr, non-xinerama setups. If it still doesn't work, please file a bug report and I'll look into this.

carga's picture
the Fiddler wrote:

Can you please test with SVN trunk?

Nice bet! Svn version is working. =)

I have realized, that there is no way to reference OpenTK.Compatibility.dll, isn't it? There are huge amount of duplicate class name conflicts in this dll.

To be exact, a lot of classes in OpenTK.Graphics namespace conflict with classes in OpenTK.Graphics.OpenGL namespace. Is it by design? Must I avoid using Compatibility assembly? At current point I'd like to use TextRenderer class to print some overlay text.

BTW, I'd like to ask you to keep TextRenderer class (maybe refactored?), because it is extremely easy to use. RenderText sample in OpenTK examples is... well... a little bit ugly. In my case I'd like to print FPS statistics which changes every frame. So it is very unnaturally to prepare full-featured texture to display 4 digits just to dispose it some milliseconds later.

I've experienced FPS about 1000 using this class, so from my POV there is no obvious performance issues with it. I feel it is very useful when printing constantly changing values (physical measurements and other realtime data streams).

Where should we expect OpenTK v1.0.1 with all this sweet bug-fixes? =)

Have a fast code!

the Fiddler's picture

OpenTK.Compatibility is there to help port applications from older OpenTK or Tao versions. It's not meant to be used for new applications.

TextPrinter used to take up about 50% of the time I put into OpenTK and I simply don't have enough time to work on it anymore. However, feel free to split and use the code from OpenTK.Compatibility (copy Source/Compatibility/Fonts and Source/Compatibility/Graphics/Text*).

I'm working on the new release.