Page 1 of 1

NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 17:56
by spillerrec
I'm trying to make a Hex viewer which only reads the portion of the file which is shown on the screen (in order to read files which are much larger than 32kB).
However after a certain point in the file it starts reading incorrectly and after a while it crashes the brick and I have to remove the batteries. SysFileSeek does not at any point return any errors.

Here is a stripped down version of my code (with TextOut() instead of FontTextOut()):

Code: Select all

void ViewBinFile(string filename){
	//Load file
	unsigned int fsize;
	byte handle;
	unsigned int result = OpenFileRead(filename, fsize, handle);
	
	FileSeekType FSTargs;
	FSTargs.FileHandle = handle;
	FSTargs.Origin = SEEK_SET;
	
	unsigned int line_amount = fsize / 8 +1;	//TODO: find actual value
	unsigned int top_line = 0;
	byte buffer[64];
	
	bool viewing_file = true;
	while(viewing_file){
		FSTargs.Length = top_line*8;
		SysFileSeek(FSTargs);
		
		unsigned int read_size = 64;
		ReadBytes(handle, read_size, buffer);
		
		//Draw everything
		ClearScreen();
		
		//Show contents of buffer
		for(int ix=0; ix<8; ix++)
			for(int iy=0; iy<8; iy++){
				TextOut(ix*12, LCD_LINE1 -iy*8, FormatNum("%02X", buffer[ix+iy*8]) );
			}
		
		
		//Do input
		while( true ){
			if(ButtonPressed(BTNLEFT, true) && top_line >0){
				top_line--;
				break;
			}
			
			if(ButtonPressed(BTNRIGHT, true) && top_line < (line_amount-8)){
				top_line++;
				break;
			}
		}
		Wait( 100 );
	}
}


task main(){
	ViewBinFile( "test.rxe" );
}
Change the filename in the main task.

Re: NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 18:39
by muntoo
A .rxe file cannot read itself. (Just in case you're trying to do this.)

Re: NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 19:02
by spillerrec
This was indeed what I was doing but the crash also happens when viewing the other programs. (It was just because it was the only .rxe file on my NXT at the moment, not that I need to be able to do it.)

Re: NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 21:13
by timpattinson
You could have used the .rtm or .sys files. I think they are actuallly RXEs

Re: NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 21:57
by spillerrec
There was only a single .sys file which size was 6 bytes and the program reads 64 bytes at once, thus there would be no seeking when using that file. I could have uploaded some random file, I had just forgotten that you can't read the currently running program.

It is still not the issue however...

Re: NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 22:02
by muntoo
timpattinson wrote:I think they are actuallly RXEs
Clarification: I meant the .rxe cannot read itself (because it already has an open handle?). It can read other .rxe files, though.

It runs OK on my NXT until it reaches the end of the file.

Try opening a large 1KB file. Scroll down for a while (to the bottom?). It freezes.

EDIT: In addition, when viewing a small >16 bytes file, it shows a bunch of "ADDBEDAB..." over and over, filling the screen. When you press the left button, it freezes. The right button doesn't seem to do anything other than refresh the drawing.

Re: NXC: Crash with SysfileSeek

Posted: 19 Jun 2011, 23:34
by spillerrec
Yes, it crashes and shows random values when it is at the end of the file. However when opening a large file (KB size) it freezes long before it is near the end. (Until I'm sure I can fix this issue I'm not going to fix the other easier one.)

Re: NXC: Crash with SysfileSeek

Posted: 20 Jun 2011, 22:05
by afanofosc
With the latest enhanced NBC/NXC firmware on my brick and the latest version of the compiler I do not experience any crashes/freezes with your program even when it reaches the end of a nearly 6kb file.

FYI, seeking only works with files opened for reading at this point. It may be that you have a slightly older and buggy version of the firmware on your NXT?

John Hansen

Re: NXC: Crash with SysfileSeek

Posted: 21 Jun 2011, 06:35
by spillerrec
I did try to update the firmware to the latest version. However while I thought I already had downloaded the latest BCC release, I had not...
It indeed works fine now. Updating your software (correctly) really does magic : )