Performance profiler - System.InvalidProgramException

Hi,

When I profile my application with the performance profiler, it consistantly fails with a System.InvalidProgramException.

I've searched around for this on Google, and various articles suggest that it is related to bad MSIL. The same application works perfectly well under the memory profiler, and opened directly (as console app).

The crash log gives the error below (I've blanked out the company in the namespace)

Event Type: Error
Event Source: .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID: 5000
Date: 31/08/2006
Time: 15:08:25
User: N/A
Computer: MYPC
Description:
EventType clr20r3, P1 utimydz5wtxl4ptvworcggqxpuyxbisk, P2 1.0.0.0, P3 44f6ed23, P4 companyx.messaging.channels, P5 1.0.0.0, P6 44f6ed19, P7 7e, P8 3e, P9 system.invalidprogramexception, P10 NIL.

My own exception handler captures the following:
Event Type: Error
Event Source: xxxx
Event Category: None
Event ID: 100
Date: 31/08/2006
Time: 15:50:13
User: N/A
Computer: my pc
Description:
Timestamp: 31/08/2006 14:50:13
Message: HandlingInstanceID: b7b8de29-ed22-425e-a240-3ed4d92e8de4
An exception of type 'System.InvalidProgramException' occurred and was caught.
08/31/2006 15:50:13
Type : System.InvalidProgramException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Common Language Runtime detected an invalid program.
Source : MyCompany.Messaging.Channels
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : Void ListenForMessages()
Stack Trace : at MyCompany.Messaging.Channels.SqlServiceBrokerUtility.ListenForMessages()
at MyCompany.Messaging.Channels.SqlServiceBrokerUtility.StartListeningForMessages() in xxxxxxxxx\MyCompany.Messaging.Channels\SqlServiceBrokerUtility.vb:line 230
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Additional Info:

MachineName : xxx
TimeStamp : 31/08/2006 14:50:13
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName : MyCompany.Integration.Host.Console.exe
ThreadIdentity :
WindowsIdentity : MyCompany\xxxxxx

Category: Errors
Priority: 0
EventId: 100
Severity: Error
Title:cccc
Machine: xxxx
Application Domain: MyCompany.Integration.Host.Console.exe
Process Id: 616
Process Name: xxxxxxx\Source\MyCompany.Integration.Host.Console\bin\Debug\MyCompany.Integration.Host.Console.exe
Win32 Thread Id: 2976
Thread Name:
Extended Properties:

After a little exploration, I found that is *only* occurs if I declare IDbConnection or SqlConnection. If I remove any references to these it behaves well.

I've tried everything I know how to in order to make this run under your profiler, but so far no joy.

Perhaps this is something seen before?

Unfortunately I won't be able to provider any source code. Maybe tonight after work I will try and create a project which will repro this.

Cheers,

Paul

Comments

  • Hi there,

    this has come up 2 or 3 times now, but no one has been able to create us a small test app. I'd be delighted if you knock one up for us - maybe a free copy of the profiler if you can!

    Many thanks,

    Tom Harris

    Red Gate Software Ltd
  • Sounds like a deal :)
  • viper, this same error happened to us as well, see http://red-gate.com/messageboard/viewtopic.php?t=2525

    For us, it always is happening when we copy a generic list to an array, so I'm wondering if that's the same for you, or if it's 2 seperate issues in the profiler.
  • I don't think it is - although I'll definately look at it. I have a feeling it's using asynchronous delegates on multiple threads. I'm slowly cutting the app away isolating it, it's taking a lot more time than I'd hoped, but I'll get there in the end :)

    Cheers,

    Paul
  • Hi there,

    I suspect that the two problems are the same or related. Many thanks for putting in the extra effort to try and create a sample for us. We will definitely go ahead and fix it once we can spot what is going wrong.

    Keep up the good work,

    Tom Harris

    Red Gate Software
  • I'm getting a similar problem in the performance profiler. From my reading of other posts, it appears to be an instrumentation issue related to generics.

    Running version 2.7.1.20.

    The exception handler captures this information:
    System.InvalidProgramException: Common Language Runtime detected an invalid program.
       at [namespace snipped].LazyList`1.Load(Store store)
       at [namespace snipped].LazyList`1.Load() in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\LazyList.cs:line 28
       at [namespace snipped].LazyList`1.get_List() in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\LazyList.cs:line 72
       at [namespace snipped].LazyList`1.get_Count() in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\LazyList.cs:line 114
       at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
       at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
       at [namespace snipped].WorklistView.LoadProviderList() in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\WorklistView.cs:line 75
       at [namespace snipped].WorklistView.OnCreateControl() in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\WorklistView.cs:line 53
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
       at System.Windows.Forms.Control.set_Visible(Boolean value)
       at [namespace snipped].[Xxx]PanelControl.SetActiveControl(String navigationName) in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\[Xxx]PanelControl.cs:line 118
       at [namespace snipped].[Xxx]WorkbenchLayoutManager.Navigation_ItemSelected(Object sender, NavigationEventArgs nea) in C:\Source\[Xxx]\Trunk\Applications\[namespace snipped]\[Xxx]WorkbenchLayoutManager.cs:line 481
       at [namespace snipped].[Xxx]NavigationBar.OnNavigationItemSelected(NavigationEventArgs nea) in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\[Xxx]NavigationBar.cs:line 121
       at [namespace snipped].[Xxx]NavigationBar.Navigation_ItemSelected(Object sender, EventArgs e) in C:\Source\[Xxx]\Trunk\Libraries\[namespace snipped]\[Xxx]NavigationBar.cs:line 112
       at Syncfusion.Windows.Forms.Tools.GroupBar.OnGroupBarItemSelected(EventArgs arg)
       at Syncfusion.Windows.Forms.Tools.GroupBar.SelectItem(Int32 newselected, Int32 oldselected)
       at Syncfusion.Windows.Forms.Tools.GroupBar.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    

    If you'd like, I can try to distill this problem into a test project.
  • Hi there,

    a test application would be great. We have a sample in VB.NET, but not C#. The problem always seems to occur with generic lists of different value types (e.g int's and bool's), but we have not got to the bottom of it yet.

    Best regards,

    Tom Harris

    Red Gate Software
  • Tom Harris wrote:
    The problem always seems to occur with generic lists of different value types (e.g int's and bool's), but we have not got to the bottom of it yet.
    FYI, my problem occurs with a list parametized with a reference type.

    I'll look into creating a test project shortly.
  • While I haven't been successful in creating a small test project, I was able to find the new code which causes the crash. To give some context, this code appears in a lazy-loaded list class which retrieves data and maps this data to an object model using reflection. The source of my troubles is a new bit of code which adjusts the parent back references of the retrieved objects in a list:
    T[] items = ...
    object _instance = ...
    string _targetProperty = ...
    
    Type type = typeof(T);
    PropertyInfo member = type.GetProperty(_targetProperty);
    foreach (T item in items)
        member.SetValue(item, _instance, null); // Crash caused by this line.
    
    Remove the marked line and the profiler does not crash. I hope this helps you narrow down the problem.

    One other note: In my efforts to narrow this down I copied the code for the above class to a test project. The identical code had absolutely no problems running there! Thinking it was related to referenced projects, I created a separate library and placed a copy of this code in it, and still there were no problems.
  • Great news guys, I spent some time to get a way to reproduce this "bad class token"/BadImageFormatException bug. The project is a simple little C# WinForms app that reproduces the problem. Run the app by itself, it works fine. Run the app under the performance profiler and Red Gate Profiler crashes.

    Here's the repro test case: Bad Class Token repro test case.zip

    p.s. Tom, I'll take you up on that free Ants Profiler offer. :-)
  • Hi there,

    thanks for all your time on this one. Something odd is clearly going on. I am able to succesfully profile the performance of your app fine with AP 2.7.1.20.

    I have not recompiled on my machine, but simply profiled the binaries you sent. I'm running XP SP2.

    Am I missing something, or can you think of anything special that you are doing?

    Regards,

    Tom
  • Tom, I've updated this post which links to a working repro case. (Direct repro case download is here) That previous repro case didn't repro because I forgot to include the .pdb for the problematic dll! :-)
  • Great work. We will go ahead and investigate this right now. This should be fixed for ANTS Profiler 3, and we will attempt to create a patch for 2.7 if it is not to difficult.

    Thanks,

    Tom Harris

    Red Gate Software
  • Hi Tom

    We had this issue in 2.7 and believed it would be fixed for 3. Having the issue means that we cannot use ANTS.

    I've just upgraded to 3.0 specifically for the resolution to this issue, but I am still having the problem.

    Any ideas ?

    Glen
  • Hi Glen,

    We fixed all the cases we could reproduce here in V3.

    One think you might wish to try is profiling in fast mode instead of detailed mode - do you still see the problem? If not, then it narrows it down to a smaller set of things that could be causing it.

    Do you think it would be possible to send us a copy of an application that exhibits this bug? We don't need the source code, just the application and associated PDB (and any libraries needed to get the thing to run).

    Alternatively, if you can come up with a small test case that fails, that'd be even better! I dare say if you can help us out here then a free ANTS Profiler T-shirt would be in order...

    Thanks,
    Rob
    Robert Chipperfield
    Red Gate
Sign In or Register to comment.