Random Exception Error when Writing a File

BlitzMax Forums/BlitzMax Beginners Area/Random Exception Error when Writing a File

sweet_kungfuman(Posted 2016) [#1]
I recently get some reports that the program that I wrote crashes in certain segments. After checking those segments it seems that the program crashes everytime it tries to write a file. Here is the file writing code, it's a simple code based on the blitzmax help tutorial. It works flawlessly for me & many people but somehow it crashes for few others. Any idea about what happen?

A note: the file in my program is guaranteed to exist, so i remove the part where it check whether the file is exist or not
		Local fileGalleryW: TStream
		fileGalleryW = WriteFile("Data.bit")
		Local i: Int
		For i = 0 To TOTAL_DATA 
			WriteInt( fileGalleryW, GalleryData[i] )
		Next
		
		CloseStream fileGalleryW



Derron(Posted 2016) [#2]
Did you check whether "fileGalleryW" is valid (or null)?

Maybe something stops "WriteFile" from returning a valid file handle (eg. write protection).


bye
Ron


Brucey(Posted 2016) [#3]
Do you check to see if the stream was created okay?

Also, is GalleryData.length > TOTAL_DATA ?


sweet_kungfuman(Posted 2016) [#4]
@Brucey & derron: yeah, the stream is not null (though it might be null to those people who has the program crashes). gallerydata length is 20, total data is 19, this is good right?

mmm ... write protection can be a suspect too. but if it's the case how am i suppose to fix it?


degac(Posted 2016) [#5]
stupid idea:
have you tried to change the file extension '.bit' in something else?
Maybe the antivirus (if you have it) *could* consider this extension dangerous...

More:
- put a Delay X just before 'CloseStream'... maybe there are some delayed write instructions (don't know)
- are you sure that the stream is not already 'opened' by something else?


Derron(Posted 2016) [#6]
(though it might be null to those people who has the program crashes)


this is what I was thinking about ...


Dunno about current OS restrictions, but wasn't there in the talks, that some newer Windows restrict write access to certain folders?

Same for linux: if a user decides not to allow your application to write to a specific file or does not allow modification within a folder... bam.


What to do then?
If something prohibits file writing, notify the user about that. You cannot do more about it. Except online storage or prompting a file chooser dialogue for the end user to choose a custom path (until you are able to write to that file - or the user aborts the whole write try/process)


For i = 0 To TOTAL_DATA 
			WriteInt( fileGalleryW, GalleryData[i] )


If you want to write everything in Gallery:
For local i:int = eachin GalleryData
			WriteInt( fileGalleryW, i )


or
For local i:int = 0 until GalleryData.length
			WriteInt( fileGalleryW, GalleryData[i] )




bye
Ron


Brucey(Posted 2016) [#7]
Where are you writing the file, exactly? Into a user data dir somewhere?

You should, at least, have code to check if the stream was created, and if not, either try again, or report an error to the user - rather than just crashing.


sweet_kungfuman(Posted 2016) [#8]
@degac: the other files who have different extension crashes too so it's not the extension. mmm ... I'm sure that the stream is not opened by something else, all the streams i created are local variables so it will dissapear after the procedure is finished. I'll try the delay though.

@derron: thanks for the codes, using length or eachin seems to be a safer solution than using constant variable!

@brucey & derron: i'm writing the files in my programs' folder, so i'm not accessing windows' directory or something. I guess i'll try to catch the exception so it can show an error message, it will be more accurate too. thanks for the suggestion!