PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Bug in CF 2.0 finally block under CE

Reply

Bug in CF 2.0 finally block under CE

 
Thread Tools Rate Thread
Old 05-03-2007, 02:03 PM   #1
Skin Diver
Guest
 
Posts: n/a
Default Bug in CF 2.0 finally block under CE


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
Old 06-03-2007, 09:28 AM   #2
=?Utf-8?B?U2ltb24gSGFydA==?=
Guest
 
Posts: n/a
Default RE: Bug in CF 2.0 finally block under CE

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
Old 11-03-2007, 12:24 PM   #3
Tomer Gabel
Guest
 
Posts: n/a
Default RE: Bug in CF 2.0 finally block under CE

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
Old 11-03-2007, 02:08 PM   #4
=?Utf-8?B?U2ltb24gSGFydA==?=
Guest
 
Posts: n/a
Default RE: Bug in CF 2.0 finally block under CE

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off