iliak's picture

OnResize before OnLoad event

In my form, I have two GlControl (from SVN). In each of them in the OnResize and OnLoad events, I put a trace log. Here's the output :

GLTileControl_Resize
Une exception de première chance de type 'System.NullReferenceException' s'est produite dans OpenTK.GLControl.dll
GLTextureControl_Load
GLTileControl_Load
GLTextureControl_Resize
GLTileControl_Resize
GLTextureControl_Resize

How about the first Resize event before the Load event ? Is it normal ? Shoudn't the Load event be the first one to be caught ? Why only one GlControl throw this resize and not the other one ?

In the designer code, GLTextureControl is "newed" first, and after GLTileControl is newed. Nothing change if I swap this order.

Here's the source code here and here for the designer.

And what about the Null Reference exception thrown ???


Comments

Comment viewing options

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

Am I correct to assume that the exception is raised in line 378 (GLTileControl.MakeCurrent())?

This "Resize-before-Load" behavior is surprising but normal (as far as I know!) It seems to affect all classes that inherit from Control, when their Size property is set before their handles are created (lines 81 and 227 of the designer code).

I am curious: can you please hook and log the HandleCreated event for the two GLControls? In theory, it should be raised before any Resize events - but this doesn't seem to be the case here.

iliak's picture

line 368 here.

Here's with a better error handler :

GLTileControl_Resize
Une exception de première chance de type 'System.NullReferenceException' s'est produite dans OpenTK.GLControl.dll
Exception "La référence d'objet n'est pas définie à une instance d'un objet."
~GLTileControl_Resize
GLTextureControl_Resize
Une exception de première chance de type 'System.NullReferenceException' s'est produite dans OpenTK.GLControl.dll
Exception "La référence d'objet n'est pas définie à une instance d'un objet."
~GLTextureControl_Resize
GLTextureControl_Load
GLTileControl_Load
GLTileControl_Resize
~GLTileControl_Resize
GLTextureControl_Resize
~GLTextureControl_Resize

Should I handle this case (it's a normal situation) or is it anormal ?

the Fiddler's picture

That's what I'm trying to determine. This is normal but the GLControl should be safe to during the Resize event (which is why I'd like to know when GL[Tile|Texture]Control.HandleCreated is called exactly).

You can work around the issue by checking whether the GL[Tile|Texture]Control.Context property is null - but I'd like to fix this inside OpenTK, if at all possible.

iliak's picture

GLTileControl_Resize
Une exception de première chance de type 'System.NullReferenceException' s'est produite dans OpenTK.GLControl.dll
Exception "La référence d'objet n'est pas définie à une instance d'un objet."
~GLTileControl_Resize
GLTextureControl_Resize
Une exception de première chance de type 'System.NullReferenceException' s'est produite dans OpenTK.GLControl.dll
Exception "La référence d'objet n'est pas définie à une instance d'un objet."
~GLTextureControl_Resize
GLTextureControl_HandleCreated
GLTextureControl_Load
GLTileControl_HandleCreated
GLTileControl_Load
GLTileControl_Resize
~GLTileControl_Resize
GLTextureControl_Resize
~GLTextureControl_Resize

the Fiddler's picture

Thanks. To be honest, I think that's rather braindead on the part of WinForms - they are raising events before the Control has been created!

I have added a workaround to rev. 2337 - the Resize event should no longer cause NullReferenceExceptions. However, you still need to keep in mind that Resize event may occur before the Load event in some (most) cases.

iliak's picture

Ok,
Should I post an issue for this or is it ok for you ?

Thanks !

the Fiddler's picture

Only if the issue still occurs. Otherwise, it's fine like this (since it's solved already).

iliak's picture

Ok, it works fine.
As a side effect, it removes an issue i had when I opened the form / closed it and after reopened it, GLControl have invalid context...

Good news :)