I am receiving this error:
A first chance exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll
and also:
Exception has been thrown by the target of an invocation.
when running this method, and the error is occurring on the CreateInstance(Type.GetType(instantiationString), parameters) call:
public static Report instantiateReport(ReportInfo reportInfo)
{
string instantiationString;
Report reportToReturn;
ReportInfo[] parameters = new ReportInfo[1];
parameters[0] = reportInfo;
instantiationString = reportInfo.Report.InstantiationPath;
reportToReturn = (Report)Activator.CreateInstance(Type.GetType(instantiationString), parameters);
return reportToReturn;
}
ReportInfo is just a custom class that contains variables that help me build the report that I am trying to instantiate and when I investigate it, everything is completely fine. The instantiationString just helps me understand which report is trying to be created at the call and that way I can use the Type.GetType() method. I really have no idea what kind of error this is or what could be going wrong. I researched it a bit but didn't receive any helpful answer so any help is appreciated.
EDIT After looking at the inner exception I am getting Object reference not set to an instance of an object. message. This is strange because neither the ReportInfo or the GetType() methods have a null reference. Say one of the variables within ReportInfo was null, could that cause an issue? It shouldn't right?
FIXED After delving into the stack trace of the inner exception I found that there was some conflicting code causing issues elsewhere. Problem solved.
Related
I am trying to get the method that called a method in an assembly that caused an error.
I have an error routine that logs errors. I would like to get the calling assembly and method along with the assembly and method where the error occurred. I got 3 out of 4, I just cant figure out how to get the calling method.
Say I have a form and a button is clicked that calls a method in another asssembly that errors. I want the assembly and method where the error occurs along with the form and click event. The click event is what I can't find.
VB
Dim Trace As System.Diagnostics.StackTrace
Trace = New System.Diagnostics.StackTrace(ex, True)
'Returns the assembly where the error occurred
Trace.GetFrame(Trace.FrameCount - 1).GetMethod.ReflectedType.FullName
'Returns the calling assembly
Assembly.GetCallingAssembly.FullName.Split(","c)(0)'Would be nice to get the class too but I can live without it.
'Method where the error occurs
ex.TargetSite.Name
'Initial calling method
???
C#
System.Diagnostics.StackTrace Trace;
Trace = New System.Diagnostics.StackTrace(ex, True);
//Returns the assembly where the error occurred
Trace.GetFrame(Trace.FrameCount - 1).GetMethod().ReflectedType.FullName;
//Returns the calling assembly
Assembly.GetCallingAssembly().FullName.Split(',')[0];//Would be nice to get the class too but I can live without it.
//Method where the error occurs
ex.TargetSite.Name;
//Initial calling method
???
I can see the source in the stack trace (Main.btnCancel_CLick but can not get it. My stacktrace frame count is 1 and I have one method in there, the one with the try catch block. Unless there's some other way to get to it that I can't figure out. Anyone know?
My application is failing only when I attempt to launch it in release mode. Debug mode works fine. I am getting an error that reads
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll
Additional information: Exception has been thrown by the target of an invocation.
Because this only happens in release, it's been a bit difficult to debug. However, if I uncheck the "Optimize code" checkbox in the project properties, I am able to see an exception thrown in this method:
private static T MakeObject<T>(Type type) where T :class
{
//Default reflective behavior to create an instance with an empty constructor
//
//*note: .GetConstructor can return null.
object obj = null;
T tObj = default(T);
ConstructorInfo ci = type.GetConstructor(TypeInfo.EmptyTypes);
if (ci != null)
{
obj = ci.Invoke(new object[] { /* Empty */});
tObj = obj as T;
}
if (tObj == null)
throw new InvalidCastException("Fatal error occurred within NavigationService (GetConstructor). Type: " + type.ToString());
return tObj;
}
on this line:
ConstructorInfo ci = type.GetConstructor(TypeInfo.EmptyTypes);
The exception reads:
An exception of type 'System.NullReferenceException' occurred in LWDCloudManager.exe but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Does anyone have any suggestions on how I might be able either fix this issue, or how to dig deeper and understand why this could be happening in release mode only?
I discovered the problem. Actually kind of a stupid problem/oversight by me. Recently my company decided that instead of throwing exceptions in internal methods, we would just use Debug.Asserts, so I went and changed all of them. One of the lines of code ahead of this issue was a debug assert wrapped around a call to a TryGetValue call on a dictionary. Because I was attempting to run in release mode, the debug.assert was not executed, thus the value was not retrieved from the dictionary resulting in a null being passed. Thanks for the help guys
While I understand what an object disposed exception is, I don't quite know why it is occurring immediately after the object is instantiated. Below is my code:
var cookiemanager = Cef.GetGlobalCookieManager();
cookiemanager.SetCookieAsync(Domain, Cookie);
The error is occurring on the second line when I attempt to call the SetCookieAsync function stating:
An exception of type 'System.ObjectDisposedException' occurred in CefSharp.Core.dll but was not handled in user code
Any help on why the object was disposed or what I can do to remedy this error would be greatly appreciated!
After some more tinkering I decided to look at the provided Cef examples where I found the following snippet:
Cef.OnContextInitialized = delegate
{
var cookiemanager = Cef.GetGlobalCookieManager();
cookiemanager.SetCookieAsync(Domain, Cookie);
};
Making this changed seemed to work!
What would be the most correct thing to do in a public API that uses Type.GetType(typeName) under the hood?
//inside a message deserializer of a framework...
....
//1. throw TypeLoadException
var type = Type.GetType(typeName,true);
//2. or..
var type = Type.GetType(typeName,false);
if (type == null)
throw new SomeMoreSpecificException("Could not find message type " + typeName +", deserialization failed");
//3. or
Type type;
try
{
type = Type.GetType(typeName,true);
}
catch(TypeLoadException x)
{
throw new SomeMoreSpecificException("some message",x);
}
Which of the above approaches would you consider the most helpful for the end user in this case?
I leaning towards the latter case here, as you get both the real TypeLoadException and some additional information specific for this very usecase.
Or should we just consider the TypeLoadException itself to be enough for the end user?
Thoughts?
[edit] for more context, see
https://github.com/akkadotnet/akka.net/issues/1279
In Akka.NET we can do "remote deployment" of actors.
if the remote system receiving the deploy request does not know about the type that should be deployed, we need to notify this somehow.
Only throwing TypeLoadException feels a bit cheap, as it does not pinpoint the problem to the remote deploy scenario.
There's not a right and wrong answer here. The trade off is having more details available in the thrown exception versus the overhead of having two exceptions throws (the original one and the custom one) instead of one.
The question is - what do you expect the user to do with the exception you're throwing? Does the "some message" you provide give more detail than the original exception? Or does it let the user do something different if it gets that specific exception type? If not, I'd just let the original exception bubble up.
Im using Emgu.CV (OpenCV), to find delta in image, but sometimes I get Access violation exception that cause my app to crash.
After digging in the debug I find that (blobs.Values):
List<CvBlob> listOfBlobs = blobs.Values.ToList();
return 1733 items, and when I do the following:
But when run through the list I get EXCEPTION:
if (resultedRectangles[j].Contains(listOfBlobs[i].BoundingBox))
I check and find the exception occurred at: i = 418 :
+BoundingBox '(new System.Collections.Generic.Mscorlib_CollectionDebugView(listOfBlobs)).Items[418].BoundingBox'
threw an exception of type
'System.AccessViolationException' System.Drawing.Rectangle
{System.AccessViolationException}
As I see the last valid value in the list is in 417.
I have 2 questions:
1. Why blobs.Values.ToList(); return such corrupt data?
2. How I can check the value before access it to prevent System.AccessViolationException ?
Are you having multiple threads in your Process? If there are multiple threads trying to initialize the List, then the list may get corrupted.
This exception is more specific to Memory related issues and you will be in a hard time to debug this, unless the all the code is in your control. The following link may help.
http://msdn.microsoft.com/en-us/library/system.accessviolationexception(v=vs.110).aspx
Me too got trapped in the same error.