It is currently Sun May 26, 2013 3:01 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: [PB 4.61B1] Crashes due to using 2dDrawing in threads
PostPosted: Thu Feb 09, 2012 1:32 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 290
OS: Ubuntu 11.10, 32-bit, all patches applied till 9th february 2012 - executed in VirtualBox.


During my attempts to produce a working sample to document an error with DrawText() used in threads, i ran into several problems which seem to point at fundamental problems of using 2dDrawing in threads on linux with PureBasic.

The following examples are compiled with "threadsafe" enabled and executable format "Linux".

Code:
Procedure.i CImage()

  ImgID.i = CreateImage(#PB_Any, 200, 200, #PB_Image_DisplayFormat)
 
  If (ImgID = 0)
    ProcedureReturn 0
  EndIf
 
  If (StartDrawing(ImageOutput(ImgID)) = 0)
    FreeImage(ImgID)
    ProcedureReturn 0
  EndIf
 
  StopDrawing()
 
  ProcedureReturn ImgID
 
EndProcedure

Procedure CImageThread(Dummy.i)
 
  Repeat
    DNum.i = Random(20)
   
    If (DNum > 0)
;      Delay(DNum)
    EndIf
   
    NewImageID.i = CImage()
   
    If (NewImageID <> 0)
      FreeImage(NewImageID)
    EndIf

  ForEver
 
EndProcedure



TestWindowID.i = OpenWindow(#PB_Any, 0, 0, 400, 400, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If (TestWindowID = 0)
  End 999
EndIf

ThreadID.i = CreateThread(@CImageThread(), 0)

If (ThreadID = 0)
  End 997
EndIf

Repeat
  Event = WindowEvent()
 
;   NewImageID.i = CImage()
;   
;   If (NewImageID <> 0)
;     FreeImage(NewImageID)
;   EndIf

  Delay(1)
 
Until Event = #PB_Event_CloseWindow


This results in the program being quickly aborted with the following error message:

Code:
The program 'Test1' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadLength (poly request too large or internal Xlib length erro'.
  (Details: serial 33020 error_code 16 request_code 55 minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)


If you don't spawn the thread or use a thread which doesnt use 2D-Drawing commands and do the same 2DDrawing in the main thread, everything works as expected.


Top
 Profile  
 
 Post subject: Re: [PB 4.61B1] Crashes due to using 2dDrawing in threads
PostPosted: Thu Feb 09, 2012 1:38 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 290
If you enable the "Delay(DNum)", the window will appear and can be used, but the moment you close the program, it will hang for a bit and then terminate with the error

Code:
Test2: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.


Example code:

Code:
Procedure.i CImage()

  ImgID.i = CreateImage(#PB_Any, 200, 200, #PB_Image_DisplayFormat)
 
  If (ImgID = 0)
    ProcedureReturn 0
  EndIf
 
  If (StartDrawing(ImageOutput(ImgID)) = 0)
    FreeImage(ImgID)
    ProcedureReturn 0
  EndIf
 
  StopDrawing()
 
  ProcedureReturn ImgID
 
EndProcedure

Procedure CImageThread(Dummy.i)
 
  Repeat
    DNum.i = Random(20)
   
    If (DNum > 0)
      Delay(DNum)
    EndIf
   
    NewImageID.i = CImage()
   
    If (NewImageID <> 0)
      FreeImage(NewImageID)
    EndIf

  ForEver
 
EndProcedure



TestWindowID.i = OpenWindow(#PB_Any, 0, 0, 400, 400, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If (TestWindowID = 0)
  End 999
EndIf

ThreadID.i = CreateThread(@CImageThread(), 0)

If (ThreadID = 0)
  End 997
EndIf

Repeat
  Event = WindowEvent()

  Delay(1)
 
Until Event = #PB_Event_CloseWindow



Top
 Profile  
 
 Post subject: Re: [PB 4.61B1] Crashes due to using 2dDrawing in threads
PostPosted: Thu Feb 09, 2012 1:48 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 290
Now adding a command to the previous example with the Delay(DNum) to actually draw a box in the thread, the behaviour becomes more erratic. 4 out of 5 times, the following example will work as if everything is fine:


Code:
Procedure.i CImage()

  ImgID.i = CreateImage(#PB_Any, 200, 200, #PB_Image_DisplayFormat)
 
  If (ImgID = 0)
    ProcedureReturn 0
  EndIf
 
  If (StartDrawing(ImageOutput(ImgID)) = 0)
    FreeImage(ImgID)
    ProcedureReturn 0
  EndIf
 
  Box(0, 0, 190, 100, RGB($ff, $ff, $ff))

  StopDrawing()
 
  ProcedureReturn ImgID
 
EndProcedure

Procedure CImageThread(Dummy.i)
 
  Repeat
    DNum.i = Random(20)
   
    If (DNum > 0)
      Delay(DNum)
    EndIf
   
    NewImageID.i = CImage()
   
    If (NewImageID <> 0)
      FreeImage(NewImageID)
    EndIf

  ForEver
 
EndProcedure



TestWindowID.i = OpenWindow(#PB_Any, 0, 0, 400, 400, "Example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If (TestWindowID = 0)
  End 999
EndIf

ThreadID.i = CreateThread(@CImageThread(), 0)

If (ThreadID = 0)
  End 997
EndIf

Repeat
  Event = WindowEvent()

  Delay(1)

Until Event = #PB_Event_CloseWindow


The Delay() inside the thread and additional 2dDrawing commands seems to affect the resulting error. By adding larger delays, the code appears to work more often. Killing the thread before the program terminates, did not seem to change anything.


Top
 Profile  
 
 Post subject: Re: [PB 4.61B1] Crashes due to using 2dDrawing in threads
PostPosted: Thu Feb 09, 2012 1:52 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 2488
Location: New Zealand
crashes on native ubuntu 11.10


Top
 Profile  
 
 Post subject: Re: [PB 4.61B1] Crashes due to using 2dDrawing in threads
PostPosted: Fri May 04, 2012 10:48 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Feb 24, 2006 9:40 am
Posts: 290
The problem still exists with the current 4.61 Beta2 x86 version in Ubuntu 12.04 LTS with all patches applied till 4th may 2012.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye