"ProfilingApiException: Invalid call sequence" when using the dotTrace SDK

(Das nachfolgende ist ein Auszug aus einem internen Support-Ticket mit JetBrains zum dotTrace SDK)

This is weird:

I’m having two independent WinForms .NET 4.5 solutions.

In both of them, I’m using the dotTrace SDK to self-profile the application.

One application works like a charm, in the other application I (nearly) always get exceptions like this:

JetBrains.Profiler.Windows.Api.ProfilingApiException: Invalid call sequence
at JetBrains.Profiler.Windows.Api.Impl.Helper.CheckHResult(HResults hr)
at JetBrains.Profiler.Windows.Api.PerformanceProfiler.Stop()

I’m using the same code in both applications (at least this is what I think). Still, one runs completely perfect, the other throws the above exception.

My question:

Can you give me any hints on what I might do wrong?

  • Initializing the profile too early/too late?
  • Initializing in the wrong thread?
  • Not using the editbin.exe /NXCOMPAT:NO executable flag on my application?
  • Stopping in the wrong thread?
  • …?

I would be very glad if you can help me bring some light into this and give me some hints.

(Das ist ein Auszug der Antwort eines Support-Mitarbeiters von JetBrain)

„Invalid call sequence“ exception means that there is no active profiling session which can be stopped by PerformanceProfiler.Stop() method.

Are you sure that you have called PerformanceProfiler.Begin() and PerformanceProfiler.Start() methods before PerformanceProfiler.Stop() method has been called? May be PerformanceProfiler.Stop() method is called twice?

(Das war meine Antwort mit der Lösung)

Since you gave me certainty that your SDK library is rock solid and has no side-effects, I started invesgitaging my own business logic:

I’ve added reference counting for starting and ending the profiling.

Finally, I found the cause and it was actually a mismatch in start/stop of the profiling

(It was actually a bit more complicated but nevertheless purely 200% my own fault).

So thanks a ton for pushing me in the right direction and solve the issue.