Strangest error/bug

BlitzMax Forums/MaxGUI Module/Strangest error/bug

Sanctus(Posted 2010) [#1]
Hey guys.
I'm trying to have a application with dynamic window(by that I mean that any number of windows of a certain type can be opened in the same time).

So I added the contentments of my window in a type, made a hook, a list..

When I create a window I call this thing
If list.Count() = 0
	AddHook(EmitEventHook, Hook)
EndIf
list.AddLast(ipw)


The hook takes care of all the windows of the same type. I also add the window object (that contains the window,canvas,whatever) in a list as you can see.

When the user presses the close button I call this:
Method Close()
	list.Remove(Self)
	If list.Count() = 0
		RemoveHook(EmitEventHook, Hook)
	EndIf
	FreeGadget(canvas)
	FreeGadget(Window)
End Method

Basically if it's the last from the list I remove the hook so I don't waste processing power.

Now if I open up any number of windows( even a single one) when I close the last one I get a random error. I say random because it's in different places every time.
Here is one line that throws an error:
Local tmpWrapper:TIntWrapper = New TIntWrapper


If I remove Freegadget(window) and freegadget(canvas) they won't appear anymore but that's not right. Since I can't just leave window wrecks and hide them.

From my experience I'd say this is a stack/heap something unbalanced. As you can see the error is thrown when allocating memory for a new object(another example of a crash place "Local t:TEvent=New TEvent")

Oh and the error is "EXCEPTION_ACCESS_VIOLATION"

Does anyone have any ideea on how to solve this?

Oh and I'm running windows 7, with latest blitzmax and maxgui.


Sanctus(Posted 2010) [#2]
After some more fiddling I noticed that if I commend this line "DrawImage(image, 0, 0, 0)" from my draw sequence the error goes away.
Though the error itself is not related to the image being drawn. That line of code does not get called afterwards.
I imagine it's something related to the wglsharelist from opengl or something. That would explain why the program crashes only when the last window is closed.


Sanctus(Posted 2010) [#3]
I found the right solution for this. If anyone needs it then the answer is below.

When you create a device a link between the gadget (canvas) and the hardware is made.
When you load/create a image it is uploaded on the hardware.

Then when you release the gadget and the image the GC collects in a possible wrong way. The device gets closed and then GC forces the image to be freed but for example in OpenGL the Delete method requires the device to free the video memory. Since there isn't any device left there's a problem and a stack/heap memory error arises.

So when you close a window with a canvas or something you have to do this
image=null
GCCollect()
FreeGadget(window)


I wrote image=null but you should free any image related to the canvas. That way when you close the last device there shouldn't be any image left.

It's a bit late and I'm very tired. If anyone feels like they should correct or add something to this then please feel free to add a comment.

Thanks.