IDE crashes when compiling

BlitzPlus Forums/BlitzPlus Beginners Area/IDE crashes when compiling

_PJ_(Posted 2014) [#1]
When I try to compile the following, the IDE crashes entirely:

Const App$="BMPThumb"
Const Version_MAJOR%=1
Const Version_MINOR%=0

Global Version#
Global Root$
Global Appdata$
Global Title$

Initialise
BuildThumbnail(Root+"test.bmp")

Function InitialiseGraphics()
	SetBuffer(DesktopBuffer())
End Function

Function InitialiseEnvironment()
	Root=Replace(CurrentDir()+"\","\\","\")
	Appdata=Replace(Replace(GetEnv("appdata")+"\","\\","\")+App+"\","\\","\")
End Function

Function InitialiseVersion()
	Version=Version_MAJOR+(Version_MINOR*0.1)
	Title=App+" "+Str(Version)
End Function

Function InitialiseRandomnisity()
	SeedRnd MilliSecs()
End Function

Function Initialise()
	InitialiseGraphics
	InitialiseEnvironment
	InitialiseVersion
	InitialiseRandomnisity
End Function

Function BuildThumbnail(FilePath$)
	Local Image=LoadImage(FilePath)
	Local Result
	If (Image)
		Local Dims=GetThumbnailDims(Image)
		Local Thumb=CreateThumbnail(Dims)
		PopulateThumbnail(Image,Thumb)
		FreeImage Image
		Result=ExportThumbnail(Thumb)
	End If	
	If (Not(Result))
		;Notify "Failed to create thumbnail for "+FilePath
	End If
End Function

Function GetThumbnailDims(Image)
	Local Width=ImageWidth(Image)
	Local Height=ImageHeight(Image)
	
	Local Ratio#=Float(Float(Width)/Float(Height))
	Local Shrink#
	
	If (Ratio>=1.0)
		Shrink=Float(Float(Width)/255.0)
	Else
		Shrink=Float(Float(Height)/255.0)
	End If
	
	If (Shrink<1.0)
		Shrink=1.0
	End If
	
	Local Scale#=Rnd(Shrink*0.25,Shrink*1.0)
	
	Local W=Int(Floor(Scale*Width))
	Local H=Int(Floor(Scale*Height))
	
	Local Ret=((W And 255) Shl 16) + (H And 255)
	Return Ret
End Function

Function CreateThumbnail(Dims)
	Local W=(Dims Shr 16) And 255
	Local H=Dims And 255
	Local Thumb=CreateImage(W,H)
	Return Thumb
End Function

Function PopulateThumbnail(Image,Thumbnail)
	Local W=ImageWidth(Thumbnail)
	Local H=ImageHeight(Thumbnail)
	
	Local X=Rand(1,ImageWidth(Image)-W)-1
	Local Y=Rand(1,ImageHeight(Image)-H)-1
	
	Local Buffer=GraphicsBuffer()
	SetBuffer(ImageBuffer(Image))
	GrabImage Thumbnail,X,Y
	SetBuffer(Buffer)
End Function

Function ExportThumbnail(Thumb)
	Local n=GetThumbnailCount()
	Local File$=Appdata$+Str(n+1)+".bmp"
	Local OK=SaveBuffer(ImageBuffer(Thumb),File)
	If (OK)
		OK=FileType(Appdata$+File)
	End If
	Return OK<>0
End Function

Function GetThumbnailCount()
	Local Dir=ReadDir(Appdata)
	Local Count=0
	Local File$=NextFile(Dir)
	While (File<>"")
		If (FileType(File)=1)
			If (Right(File,4)=".bmp")
				Count=Count+1
			End If
		End If
		File=NextFile(Dir)
	Wend
	Return Count
End Function
			


Can anyone see what's up with the code?


Chalky(Posted 2014) [#2]
What do you mean by "the IDE crashes entirely"? When I try to compile your code I get a "Function 'desktopbuffer' not found" error in both the Blitz3D IDE and Protean, but neither IDE crashes.


videz(Posted 2014) [#3]
If I'm not mistaken, looks like this is blitzplus code which has the DesktopBuffer() function..


Chalky(Posted 2014) [#4]
Doh - that'll teach me not to post without checking which subforum I'm in! :o/

I compiled it in the BlitzPlus IDE and it ran without crashing there either (although it did fail with the "Failed to create thumbnail" message [once I uncommented it]). However, seeing it needs a "test.bmp" to process [which didn't exist on my system] - I created one, and then the program did indeed trigger a crash.

I think I have traced this to GetThumbnailCount(), which looks for BMP files in folder "C:\Documents and Settings\<username>\Application Data\BMPThumb\" [via ReadDir(Appdata)]. Since this folder does not exist on my system, Dir is empty. However, you don't check this before calling NextFile(Dir). The following mod stops the crash - but still doesn't create a thumbnail (possibly because the target folder does not exist?).
Function GetThumbnailCount()
	Local Dir=ReadDir(Appdata)
	Local Count=0
	If Dir Then
		Local File$=NextFile(Dir)
		While (File<>"")
			If (FileType(File)=1)
				If (Right(File,4)=".bmp")
					Count=Count+1
				End If
			End If
			File=NextFile(Dir)
		Wend
	EndIf
	Return Count
End Function

Hope that helps.


_PJ_(Posted 2015) [#5]

I think I have traced this to GetThumbnailCount(), which looks for BMP files in folder "C:\Documents and Settings\<username>\Application Data\BMPThumb\" [via ReadDir(Appdata)]. Since this folder does not exist on my system, Dir is empty. However, you don't check this before calling NextFile(Dir). The following mod stops the crash - but still doesn't create a thumbnail (possibly because the target folder does not exist?).


Interesting, thanks for all your help, I think you've identified the issue spot on!
Yes, the thumb wont be created if the program halts at the count function - I had expeced this would simply create an error due to null handle but it seems this is not picked up.

I'll have a good luck later on when I get home, but I'm pretty sure you've nailed it! I really should have ensured the folder exists first and created the subdirectory if necessary!