none
Sharing Voilation - Threading Issue?

    Question

  • I have a wrapper for the SaveDialog control that is specific to my needs, but it is not working correctly.
    To save time and space below is the wrapper class, with nothing in it, but it still does not work.
    It appears that Silverlight is holding a stream open, and trying to release it using another thread.

    Steps to recreate this issue.

    1. Place this class in you silverlight app (Below)
    2. Put a button on the screen.
      In the code behind for the button click do this.
      var TestBug = new SaveFileDialogBug("Excel File (*.xls)|*.xls", ".xls");
    3. Create a excel file, or use one you have. This can be any file by the way.
    4. Open the file (The one you created, this is a sharing issue, as above it would be the excel file)
    5. Run the Silverlight App.
    6. Press the button
    7. Save as your excel file (The one you have opened)
    8. It should work as expected, no exception as the sharing voilation is caught.
    9. But, do steps 6 to 7 a couple of times and silverlight throws an exception.
      On my system it is every two - three times.

     

    public class SaveFileDialogBug
            {
                private System.Windows.Controls.SaveFileDialog sfd = null;
                private System.IO.Stream DataFileStream;
    
                public SaveFileDialogBug(string Filter, string DefaultExt)
                {
                    sfd = new System.Windows.Controls.SaveFileDialog { Filter = Filter, DefaultExt = DefaultExt };
    
                    bool? sd = sfd.ShowDialog();
    
                    if (sd.HasValue && sd.Value)
                    {
                        try
                        {
                            DataFileStream = sfd.OpenFile();
    
                            sfd = null;
    
                            if (!DataFileStream.CanWrite)
                            {
                                DataFileStream.Dispose();
                                DataFileStream = null;
                                return;
                            }
                        }
                        catch (System.IO.IOException ex)
                        {
                            sfd = null;
                            return;
                        }
                    }
                }
            }

     

     

    Thursday, November 17, 2011 12:30 AM

Answers

  • I found this bug in .net also, and I know of another one which causes the designer to crash but it works fine in runtime.

    And there is also another bug in silverlight exception handling that causes the application to crash.

     

    [REG:110121401613631] Professional/Visual Studio 2010 Premium/customer is having issue with visual studio 2010 always crashing

     

    ** Email Message

    Hi
    Alistair,

     

    Thank you
    for your reply. I am now going ahead to archive the case based on case status
    and the communication. This case will be non-decrement and you will not be
    charged. If this is pr-mature or you are not very satisfied with some aspects
    of this case, please let me know and I will be glad to follow up.

     

    My goal is
    to make certain that you are totally satisfied with my service. If you have any
    concerns or feedback about the service you have received throughout this case,
    you can send it to me, or to my manager, Mr. Fan by emailing to lmfan@microsoft.com. Thank you

     

    Below is
    the case summary for your reference.

     

    Problem:

    When work
    on VS.NET 2010, there will be IDE Crash when build Silverlight project after
    modified the XAML file.

     

    Deliverables:

    Based on our analysis of dump and local
    repro. Original Silverlight core component CCoreServices got destroyed due to DesignerPane.ReloadDesigner
    method. However, the managed object MS.Internal.NativeObjectSafeHandle should follow GC lifecycle.

     

    When MS.Internal.NativeObjectSafeHandle got Finalized, the Finalizer method referred to
    native C++ object CRoutedEventArgs which has m_pCore to invalid CCoreServices object. Thus there is AV, IDE crashes.

     

    It is confirmed as a product issue, because
    the problem does not appear in SL5, which will be finally released. After
    getting your confirmation of the workaround(using VS.NET 2010 sp1+SL 5 beta),
    we now archive this case.

    Monday, November 21, 2011 3:04 PM

All replies

  • Hi, 

    your class does not close the Stream in case it is writeable, right? You should do it in finally clause e.g. always.

    Regards, Greg

    Thursday, November 17, 2011 5:29 AM
  • Well spotted, in my complete class it is handled.

    But in the example, the Stream to close is null if a sharing voilation is thrown.

    So there is nothing to close.

    Did you find it throws an exception back to the HTML scource?
    After doing steps 6-7 a few times.

    Which suggest when GC collects it is closing the underlining stream, which i cannot get access to, if an exception is thrown.

    Thursday, November 17, 2011 11:26 AM
  • So there is nothing to close.

    I mean case no exception is thrown. Stream is in private variable DataFileStream, thus will be closed by GC at some time later, you cannot do it. When you call constructor of this class several times you end with a plenty of open handles and there's no way to close them. 

    in my complete class it is handled.
    If so, you describe an another case with an another SaveFileDialogBug class. Otherwise pls explain.

    Thursday, November 17, 2011 12:49 PM
  • Hi, not sure if you have followed the steps I suggested.

    Try this. Remove the class SaveFileDialogBug

    Under the click event add ths below code.

    Click the button, and save to an excel file that is already open.

    This will cause the sharing voilation, which is expected, and caught.

    But if you do this a couple of times it throws an exception outside the button click event, so it looks like the System.Windows.Controls.SaveFileDialog is holding a handle open and it is trying to close it on another thread.

    Does this help

                var sfd = new System.Windows.Controls.SaveFileDialog { Filter = "Excel File (*.xls)|*.xls", DefaultExt = ".xls" };
    
                bool? sd = sfd.ShowDialog();
    
                if (sd.HasValue && sd.Value)
                {
                    try
                    {
                        using (var DataFileStream = sfd.OpenFile())
                        {
                        }
    
                        sfd = null;
                    }
                    catch (System.IO.IOException ex)
                    {
                        // *************************************************************
                        // This is where is exception should be caught
    
                        sfd = null; 
                        return;
                    }
                }
    Thursday, November 17, 2011 1:29 PM
  • Sorry, but I have no exception with this version of handler. I've did it 20 times e.g. 60 clicks! Nothing.

    Thursday, November 17, 2011 2:28 PM
  • Many Thanks,

    I am used Silverlight 4, in debug mode.

    I did not think to try it in runtime, I will publish it and have a test tonight as my clients are using the system with the old code.

    Thursday, November 17, 2011 2:37 PM
  • I suppose, your using expression solves the problem. It disposes stream, when out of scope, automatically. Should it be solved, pls mark.

    Thursday, November 17, 2011 3:05 PM
  • Thanks, using VS 2010

    I will check it tonight, around 8 hours time.

    Have to work on another project and I have meetings all day.

    Thursday, November 17, 2011 3:08 PM
  • I enjoy being useful. Should you need support, post it here.

    Thursday, November 17, 2011 3:48 PM
  • Hi, still having not luck, below is the silverlight error message.

    As you can see it is within the system code.

    Error: Unhandled Error in Silverlight Application
    Code: 4004   
    Category: ManagedRuntimeError      
    Message: System.InvalidOperationException: This operation can only occur on the UI Thread.
       at System.Windows.Hosting.NativeHost.VerifyThread()
       at System.Windows.SaveFileStream.Dispose(Boolean disposing)
       at System.IO.FileStream.Finalize()    

     

    Any ideas?

    Sunday, November 20, 2011 10:28 PM
  • I changed the below code, and put a break point on e.handled=true

    After pressing the button to cause the sharing voilation three times it reached the break point.

    If i presssed F5 -> Run, it hit the break point again.

    Then F5, it hi the below break point again.

    All three exceptions, reached here.

    The code below programs around the error reaching the user, but it looks like a .net issue to me.

     

    public static void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                if (e.ExceptionObject.StackTrace.Contains("at System.IO.FileStream.Finalize()"))
                {
                    e.Handled = true;
                    return;
                }
             }

     

    Sunday, November 20, 2011 10:46 PM
  • You get this exception running version mit "using"? Seems like behavior caused by SaveFileDialogBug e.g. no closing of the stream. I wrote to you here http://social.msdn.microsoft.com/Forums/en-US/silverlightbugs/thread/d2281b47-b929-44f0-8c48-9a17fd1a1076#9ff09af5-8f85-469d-918f-234ef8fe07c7 that evenually it will be closed by the GC. Of course is not on UI thread.

    Does it describe the issue you have now?

    Monday, November 21, 2011 4:21 AM
  • There is no stream to close when there is a sharing voilation, no stream is returned for me to close, it throws an exception, as in the example below, the line "DataFileStream.Close();" is never reached and the variable DataFileStream is never assigned a value.

     

    var sfd = new System.Windows.Controls.SaveFileDialog { };
    
    sfd.ShowDialog(); 
    
    try 
    { 
          var DataFileStream = sfd.OpenFile(); 
          DataFileStream.Close();
     } 
    catch (System.IO.IOException ex) 
    { 
          return; 
    } 
    Monday, November 21, 2011 2:10 PM
  • Tell me what happens in a "normal" case with the stream when Open succeeds? When will it be closed. I think - never -  and it ist the cause of the following exceptions. Sorry, but I believe more in error in the logic of the code then in a bug in FileOpenDialog. 

    Monday, November 21, 2011 2:33 PM
  • it works fine, it is closed after I have written to the stream.

    You are missing the bug, it happens when there is a sharing voilation, everything else is fine.

    Monday, November 21, 2011 2:59 PM
  • I found this bug in .net also, and I know of another one which causes the designer to crash but it works fine in runtime.

    And there is also another bug in silverlight exception handling that causes the application to crash.

     

    [REG:110121401613631] Professional/Visual Studio 2010 Premium/customer is having issue with visual studio 2010 always crashing

     

    ** Email Message

    Hi
    Alistair,

     

    Thank you
    for your reply. I am now going ahead to archive the case based on case status
    and the communication. This case will be non-decrement and you will not be
    charged. If this is pr-mature or you are not very satisfied with some aspects
    of this case, please let me know and I will be glad to follow up.

     

    My goal is
    to make certain that you are totally satisfied with my service. If you have any
    concerns or feedback about the service you have received throughout this case,
    you can send it to me, or to my manager, Mr. Fan by emailing to lmfan@microsoft.com. Thank you

     

    Below is
    the case summary for your reference.

     

    Problem:

    When work
    on VS.NET 2010, there will be IDE Crash when build Silverlight project after
    modified the XAML file.

     

    Deliverables:

    Based on our analysis of dump and local
    repro. Original Silverlight core component CCoreServices got destroyed due to DesignerPane.ReloadDesigner
    method. However, the managed object MS.Internal.NativeObjectSafeHandle should follow GC lifecycle.

     

    When MS.Internal.NativeObjectSafeHandle got Finalized, the Finalizer method referred to
    native C++ object CRoutedEventArgs which has m_pCore to invalid CCoreServices object. Thus there is AV, IDE crashes.

     

    It is confirmed as a product issue, because
    the problem does not appear in SL5, which will be finally released. After
    getting your confirmation of the workaround(using VS.NET 2010 sp1+SL 5 beta),
    we now archive this case.

    Monday, November 21, 2011 3:04 PM