PC Review


Reply
Thread Tools Rate Thread

Bug in CF 2.0 finally block under CE

 
 
Skin Diver
Guest
Posts: n/a
 
      5th Mar 2007
Here is an example of what looks to be a bug in the Compact Framework when
executing under Windows CE.

If you run it on XP, you get the right result. "Done. state=Finished".
If you run it on the emulator, you get the wrong result. "Done.
state=Inside the lock".
This is very strange, and could be very bad because part of finally code
won't be executed.



using System;
using System.Threading;

namespace ConsoleApplication9
{
class Program
{
static public string state = string.Empty;
private static readonly object theLock = new object();
static void Main(string[] args)
{
try
{
try
{
state = "Starting";
throw new Exception("Exception!!!");
}
finally
{
lock (theLock)
{
state = "Inside the lock";
}
state = "Finished";
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("Done. state=" + state);

Thread.Sleep(Timeout.Infinite);
}
}
}





 
Reply With Quote
 
 
 
 
=?Utf-8?B?U2ltb24gSGFydA==?=
Guest
Posts: n/a
 
      6th Mar 2007
This does look like a bug as I have just tried it using CF2 on PPC 2003 SE. I
suggest you raise it with the CF team.
However, not sure why you'd ever try to lock a readonly object.

Simon.
"Skin Diver" wrote:

> Here is an example of what looks to be a bug in the Compact Framework when
> executing under Windows CE.
>
> If you run it on XP, you get the right result. "Done. state=Finished".
> If you run it on the emulator, you get the wrong result. "Done.
> state=Inside the lock".
> This is very strange, and could be very bad because part of finally code
> won't be executed.
>
>
>
> using System;
> using System.Threading;
>
> namespace ConsoleApplication9
> {
> class Program
> {
> static public string state = string.Empty;
> private static readonly object theLock = new object();
> static void Main(string[] args)
> {
> try
> {
> try
> {
> state = "Starting";
> throw new Exception("Exception!!!");
> }
> finally
> {
> lock (theLock)
> {
> state = "Inside the lock";
> }
> state = "Finished";
> }
> }
> catch (Exception e)
> {
> Console.WriteLine(e.ToString());
> }
> Console.WriteLine("Done. state=" + state);
>
> Thread.Sleep(Timeout.Infinite);
> }
> }
> }
>
>
>
>
>
>

 
Reply With Quote
 
Tomer Gabel
Guest
Posts: n/a
 
      11th Mar 2007
Hello Simon,

I don't have anything meaningful to add to the discussion (seeing as this
is apparently a bug in the CF CLR), but I did want to mention that objects
that do not change across the lifetime of a class instance (such as lock
objects of the kind used below) - particularly static ones - make perfect
sense to mark with readonly; it can save you a whole heap of accidental trouble
later if you've erroneously reinitialized the field somewhere.

Regards,
Tomer Gabel (http://www.tomergabel.com)
Monfort Software Engineering Ltd. (http://www.monfort.co.il)


> This does look like a bug as I have just tried it using CF2 on PPC
> 2003 SE. I
> suggest you raise it with the CF team.
> However, not sure why you'd ever try to lock a readonly object.
> Simon.
> "Skin Diver" wrote:
>> Here is an example of what looks to be a bug in the Compact Framework
>> when executing under Windows CE.
>>
>> If you run it on XP, you get the right result. "Done.
>> state=Finished".
>> If you run it on the emulator, you get the wrong result. "Done.
>> state=Inside the lock".
>> This is very strange, and could be very bad because part of finally
>> code
>> won't be executed.
>> using System;
>> using System.Threading;
>> namespace ConsoleApplication9
>> {
>> class Program
>> {
>> static public string state = string.Empty;
>> private static readonly object theLock = new object();
>> static void Main(string[] args)
>> {
>> try
>> {
>> try
>> {
>> state = "Starting";
>> throw new Exception("Exception!!!");
>> }
>> finally
>> {
>> lock (theLock)
>> {
>> state = "Inside the lock";
>> }
>> state = "Finished";
>> }
>> }
>> catch (Exception e)
>> {
>> Console.WriteLine(e.ToString());
>> }
>> Console.WriteLine("Done. state=" + state);
>> Thread.Sleep(Timeout.Infinite);
>> }
>> }
>> }



 
Reply With Quote
 
=?Utf-8?B?U2ltb24gSGFydA==?=
Guest
Posts: n/a
 
      11th Mar 2007
I'm not saying the object should be not marked readonly, rather there is
little point is locking a readonly object. In fact this just increases a
unnessessary performance overhead. The only other reason you might use lock
in this kind case which acts as a dummy variable is if you only ever want one
thread to access a routine at once which could be database access, global
variables or just a procedure that should never be run by more than one
thread etc.

Anyway the example code is contained within the Main method so would never
have more than one thread.

Simon.


"Tomer Gabel" wrote:

> Hello Simon,
>
> I don't have anything meaningful to add to the discussion (seeing as this
> is apparently a bug in the CF CLR), but I did want to mention that objects
> that do not change across the lifetime of a class instance (such as lock
> objects of the kind used below) - particularly static ones - make perfect
> sense to mark with readonly; it can save you a whole heap of accidental trouble
> later if you've erroneously reinitialized the field somewhere.
>
> Regards,
> Tomer Gabel (http://www.tomergabel.com)
> Monfort Software Engineering Ltd. (http://www.monfort.co.il)
>
>
> > This does look like a bug as I have just tried it using CF2 on PPC
> > 2003 SE. I
> > suggest you raise it with the CF team.
> > However, not sure why you'd ever try to lock a readonly object.
> > Simon.
> > "Skin Diver" wrote:
> >> Here is an example of what looks to be a bug in the Compact Framework
> >> when executing under Windows CE.
> >>
> >> If you run it on XP, you get the right result. "Done.
> >> state=Finished".
> >> If you run it on the emulator, you get the wrong result. "Done.
> >> state=Inside the lock".
> >> This is very strange, and could be very bad because part of finally
> >> code
> >> won't be executed.
> >> using System;
> >> using System.Threading;
> >> namespace ConsoleApplication9
> >> {
> >> class Program
> >> {
> >> static public string state = string.Empty;
> >> private static readonly object theLock = new object();
> >> static void Main(string[] args)
> >> {
> >> try
> >> {
> >> try
> >> {
> >> state = "Starting";
> >> throw new Exception("Exception!!!");
> >> }
> >> finally
> >> {
> >> lock (theLock)
> >> {
> >> state = "Inside the lock";
> >> }
> >> state = "Finished";
> >> }
> >> }
> >> catch (Exception e)
> >> {
> >> Console.WriteLine(e.ToString());
> >> }
> >> Console.WriteLine("Done. state=" + state);
> >> Thread.Sleep(Timeout.Infinite);
> >> }
> >> }
> >> }

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
finally block =?Utf-8?B?R2Vvcmdl?= Microsoft C# .NET 7 24th Aug 2006 04:05 AM
finally block is not executed after StackOverflowException Vladimir Arkhipov Microsoft C# .NET 3 23rd Jul 2004 09:39 AM
should finally block be the last executing block in a function? Microsoft C# .NET 1 6th Apr 2004 08:33 PM
'finally' block Brian Microsoft C# .NET 3 30th Dec 2003 09:53 PM
Thread.Abort() and finally block Bill Microsoft C# .NET 16 18th Dec 2003 12:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:25 AM.