Program Crashes

Just starting out? Need help? Post your questions and find answers here.
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Program Crashes

Post by matalog »

Any idea why this crashes, at different points during execution?

Code: Select all

EnableExplicit
#width = 1000
#height = 1000
#width1 = 26000
#height1 = 26000
#max=1000
#ImgGadget = 0
Global Dim cols.i(9999)
Global.i action=1,save=0,loops=1,count=0,erase=0
Global.i timer=0,tot=0
Global.f re,gr,bl,wi,he,wi1,xo,yo,col,h,w
Global.f x1o,x2o,y1o,y2o
Global.i x,y,t,time,thr1
Global.i x1,y1,x2,y2,len,minus,alp,f,p,p1,p1m,p2m,p3m,p4m
Global.s f$,g$,hp,pstr
Global.i pnum=0,pn
Global.d p0=0,p1=0,p2=0,p3=0
Global Dim pixnum.i(#width1,#height1)
Global r.i=0,g.i=0,b.i=0
Global.i r=0,an=0,ty=0,np=0,alp=0,bufchange=0
Global.b by=0,tby=0
Global *buf
ReadFile (0, "D:\Patterns\patternplot.bin")
*buf = AllocateMemory (1000000000)
ReadData (0, *buf, 1000000000)
x=#width1/2:y=#height1/2
Enumeration
  #Menu_Escape
EndEnumeration
count=1
wi=#width1/2
wi1=#width1/2.5
he=#height1/2
tot=6
x1=Random(wi+50,wi-50)
y1=Random(he+50,he-50)
x2=Random(wi+50,wi-50)
y2=Random(he+50,he-50)
re=Random(255,0)
gr=Random(255,0)
bl=Random(255,0)
col=0
f=0
h=15
x=1
w=#width1/2-800
h=#height1/2+4000
Define.i Event, EventGadget, copy, quit, main, imagesc
main = OpenWindow(#PB_Any, 50, 0, #width-23, #height, "Drawing",#PB_Window_MinimizeGadget)
Global image = CreateImage(#PB_Any, #width1,#height1) 
ImageGadget(#ImgGadget, 0, 0, #width1, #height1, ImageID(image))
CreateStatusBar(0, WindowID(main))
imagesc = CreateImage(#PB_Any, #width-StatusBarHeight(0),#height-StatusBarHeight(0))
ImageGadget(#ImgGadget, 0, 0, #width-StatusBarHeight(0), #height-StatusBarHeight(0), ImageID(imagesc))
AddKeyboardShortcut(main, #PB_Shortcut_Escape, #Menu_Escape)

UseJPEGImageEncoder()
AddWindowTimer(main, 1, 100)
AddStatusBarField(130)
AddStatusBarField(130)
AddStatusBarField(130)
AddStatusBarField(130)
AddStatusBarField(130)
AddStatusBarField(130)
AddStatusBarField(130)


Procedure.l HSV(Hue.f, Saturation.f, Value.f)
  alp=2
	Protected H.i = Int(Hue/60)
	Protected f.f = (Hue/60-H)
	Protected S.f = Saturation/100
	If S > 1.0 : S = 1.0 : ElseIf S < 0.0 : S = 0.0 : EndIf
	Protected V.f = Value * 2.55
	If V > 255 : V = 255 : ElseIf V < 0 : V = 0 : EndIf
	Protected p.i = V * (1-S)
	Protected q.i = V * (1-S*f)
	Protected t.i = V * (1-S*(1-f))
	Select H
		Case 1 : ProcedureReturn RGBA(q,V,p,alp)
		Case 2 : ProcedureReturn RGBA(p,V,t,alp)
		Case 3 : ProcedureReturn RGBA(p,q,V,alp)  
		Case 4 : ProcedureReturn RGBA(t,p,V,alp)
		Case 5 : ProcedureReturn RGBA(V,p,q,alp)  
		Default : ProcedureReturn RGBA(V,t,p,alp)
	EndSelect
EndProcedure


For t=1 To 200
  cols(t)=HSV(360*t/200, 255, 255)
  Debug cols(t)
  
Next

Procedure SDRAW(parameter)
  Shared image,copy,imagesc
  
  Repeat 
    
    StartDrawing(ImageOutput(image))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    
    Repeat 
       by.b= PeekB(*buf+x)

        
        For time=0 To 3

           tby = by & 3

           p=tby

           If w>2 And w<#width1-2 And h>2 And h<#height1-2
             Plot(w,h,cols(loops))
             EndIf

        
        If p=0
          w=w+0.08
          p0=p0+1
          Goto skip
        EndIf
        
        If p=1
          p1=p1+1
          w=w-0.08
          Goto skip
        EndIf
        
        If p=2
          p2=p2+1
          h=h+0.08
          Goto skip
        EndIf
        
        If p=3
          p3=p3+1
          h=h-0.08
        EndIf
        
        
        
        skip:      
        
        
            by >> 2
          Next
          x=x+1
      
      action=action+1
    Until action=100000000

    action=1
    count=count+1
    
    StopDrawing()
    
    
    
    loops=loops+1
    CopyImage(image, copy)
    ResizeImage(copy, #width-StatusBarHeight(0), #height-StatusBarHeight(0))
    StartDrawing(ImageOutput(imagesc))
    DrawImage(ImageID(copy), 0, 0)
    StopDrawing()
    SaveImage(image,"Petterndraw - "+FormatDate("%yyyy%mm%dd_%hh%ii%ss", Date())+Str(loops)+".jpg", #PB_ImagePlugin_JPEG,99)
    StatusBarText(0, 0, "Saved Image " + Str(save), #PB_StatusBar_Right)
    save=save+1
    
    SetGadgetState(#ImgGadget, ImageID(imagesc))
    StatusBarText(0, 1, Str(bufchange), #PB_StatusBar_Center)
    StatusBarText(0, 2, Str(x), #PB_StatusBar_Center)
    StatusBarText(0, 3, Str(p0), #PB_StatusBar_Center)
    StatusBarText(0, 4, Str(p1), #PB_StatusBar_Center)
    StatusBarText(0, 5, Str(p2), #PB_StatusBar_Center)
    StatusBarText(0, 6, Str(p3), #PB_StatusBar_Center)
    
        If x>=1000000000
      ReadData (0, *buf, 1000000000)
      x=1
      bufchange=bufchange+1
      EndIf
    
    
  Until loops = 101*100
  
  CloseFile (0)
EndProcedure


thr1=CreateThread(@SDRAW(),1)

Repeat
  
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_Menu
      Select EventMenu()

        Case #Menu_Escape
          quit = #True

      EndSelect
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      Select EventGadget        
      EndSelect
    Case #PB_Event_CloseWindow
      Quit=#True
  EndSelect

Until quit
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Program Crashes

Post by jacdelad »

First, you never check whether the memory block could be allocated, second, you don't check whether the file could be opened, and third, you should split your read operation into smaller pieces. To read 1GB is not ideal.
Last edited by jacdelad on Fri May 31, 2024 10:19 pm, edited 1 time in total.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Program Crashes

Post by mk-soft »

- Not enough memory
- Gadget functions are not thread save
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Re: Program Crashes

Post by matalog »

jacdelad wrote: Fri May 31, 2024 10:18 pm First, you never check whether the memory block could be allocated, second, you don't check whether the file could be opened, and third, you should split your read operation into smaller pieces. To read 1GB is not ideal.
Thanks. This always allocates the memory fine at least for the first 1000000000 bytes. and the file opens fine.

Given those things, it still crashes at random points during the process, mostly during the first 1000000000 bytes.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Program Crashes

Post by jacdelad »

You still should check all return values. Maybe this will narrow it down to some point.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
matalog
Enthusiast
Enthusiast
Posts: 305
Joined: Tue Sep 05, 2017 10:07 am

Re: Program Crashes

Post by matalog »

I added a check to the file reading and memory allocation, and as expected the work fine. I reduced the amount of memory used by a factor of 10 and that seems to be helping. It hasn't crashed now in 76 iterations vs 6 to 14 iterations before. Thanks for the help.
Post Reply