简体   繁体   中英

Visual studio source of message in output window

Let's say I have a message in my output window (Showing output from Debug )

Exception thrown: 'System.InvalidOperationException' in mscorlib.dll

My application doesn't throw an exception, just displays that message and carries on. The exception occurs when I make a call to a method in an imported DLL which is a C++ module (my application is C#). The method still appears to function correctly in spite of this message appearing.

My guess is that that module is handling the exception and then displaying that message, but I want to be sure that this is the case and it's nothing to do how I've imported it or marshalled the data (or that custom marshaller).

(My code:

[DllImport("theExternalModule.dll", EntryPoint = "ReadData", SetLastError = true)]
[return: MarshalAs(UnmanagedType.U4)]
private static extern UInt32 ReadData([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(JaggedArrayMarshaler))] Int16[][] data,
                                      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(JaggedArrayMarshaler))] Int16[][] dataOrig,
                                      [MarshalAs(UnmanagedType.U2)] Int16 buffsize,
                                      ref int smpNum);

and

resultCode = ReadData(_buffer, _origBuffer, bufferSize, ref sampleNumber);

(when I step through this line in the debugger, the message is displayed)

My question is, is there a way of getting the output window to tell me what module or method caused that message to be displayed?

This problem usually happens when you change a collection after an enumerator has been created. Your original question don't have enough code to assert that, but you can read about this problem here and here .

Here's an example (taken from one of the article) that would throw that exception :

List<Book> books = new List<Book>();
books.Add(new Book("The Stand", "Stephen King"));
books.Add(new Book("Moby Dick", "Herman Melville"));
books.Add(new Book("Fahrenheit 451", "Ray Bradbury"));
books.Add(new Book("A Game of Thrones", "George R.R. Martin"));
books.Add(new Book("The Name of the Wind", "Patrick Rothfuss"));

Book newBook = new Book("Robinson Crusoe", "Daniel Defoe");
foreach (var book in books)
{
  if (!books.Contains(newBook))
  {
    books.Add(newBook);  // Throws a InvalidOperationException in mscorlib.dll
  }
}

You may also wish to take a look at the InvalidOperation class on MSDN for further causes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM