[Mono-list] Problem with while loop and Main returning void

Dietmar Maurer dietmar@ximian.com
03 Jul 2002 09:48:29 +0200


This looks like a mcs bug. mcs don't generate a return statement at the
end of Main:

    .method public static 
           default void Main()  cil managed 
    {
        // Method begins at RVA 0x20f4
	.entrypoint
	// Code size 25 (0x19)
	.maxstack 8
	IL_0000: br IL_0005

	IL_0005: ldstr "Breaking..."
	IL_000a: call void class [corlib]System.Console::WriteLine(string)
	IL_000f: br IL_0019

	IL_0014: br IL_0005

    } // end of method default void Main() 

csc generates correct code wit a return at the end:

    .method public static  hidebysig 
           default void Main()  cil managed 
    {
        // Method begins at RVA 0x2050
	.entrypoint
	// Code size 17 (0x11)
	.maxstack 1
	IL_0000: br.s IL_000e

	IL_0002: ldstr "Breaking..."
	IL_0007: call void class [corlib]System.Console::WriteLine(string)
	IL_000c: br.s IL_0010

	IL_000e: br.s IL_0002

	IL_0010: ret 
    } // end of method default void Main() 

csc also generates much shorter code!

- Dietmar

On Wed, 2002-07-03 at 08:29, Leigh Dyer wrote:
> Hey guys,
> 
> I think I've found a minor bug in either mcs or the runtime. I was
> writing a small app that has just has a Main() and a small while(true)
> loop, with a break condition inside it. However, when I compiled my app
> with mcs and ran it with mono, after breaking out of the while(true)
> loop my program kept running - it seems to just keep re-running Main().
> Running with mint exits at the end of the first run, but with a
> "Trace/breakpoint trap" message.
> 
> However, if I change my Main() so that it returns int instead of void,
> and add a "return 0;" to the end, everything works as expected.
> 
> I tried to isolate the problem with a small test case, and while it
> doesn't exhibit exactly the same behaviour, it still doesn't work as
> expected with Main() returning void - mint segfaults and mono returns
> the following error:
> 
> ** (process:18686): WARNING (recursed) **: unhandled exception
> System.NullReferenceException: "A null value was found where an object
> instance was required"
> 
> RESULT: -1
> 
> and then needs to be sent a ctrl-c.
> 
> I'm running the mono CVS debs from http://www.atoker.com/mono/ as of
> yesterday on a Debian sid system.
> 
> I've attached both my small test case and the original app (it's very
> small - all it does is iterate over the bytes of a text file looking for
> non-ascii chars) - I hope they're of some use.
> 
> Thanks
> Leigh
> ----
> 

> using System;
> 
> public class BreakTest
> {
> 	public static void Main()
> 	//public static int Main()
> 	{
> 		while(true)
> 		{
> 			Console.WriteLine("Breaking...");
> 			break;
> 		}
> 		//return 0;
> 	}
> }
> ----
> 

> using System;
> using System.IO;
> 
> public class NonAscii
> {
> 	public static void Main(string[] args)
> 	//public static int Main(string[] args)
> 	{
> 		FileStream s = new FileStream(args[0], FileMode.Open, FileAccess.Read);
> 		int line=0, ch=0;
> 		int x=0;
> 		
> 		while(true)
> 		{
> 			int c = s.ReadByte();
> 			Console.WriteLine("char " + x + ": " + c);
> 			if(c == -1) break;
> 
> 			if(c == '\n')
> 			{
> 				line++; ch=0;
> 			} else {
> 				if(c > 127)
> 				{
> 					Console.WriteLine("Non-ASCII char: line " + line + ", char " + ch);
> 				}
> 				ch++;
> 			}
> 			x++;
> 		}
> 		//return 0;
> 	}
> }