I have a VB.NET application that seems to run perfectly in the VS2012 IDE. It processes thousands of records. When I compile it for release it will process random numbers of records (<200) and dies with "Object reference not set to an instance of an object."
It always dies in an external C++ DLL that I call which also does call backs to my code. The call backs from unmanaged code require the use of delegates. I am not familiar with the intricacies of delegates never having to use them before.
In my research I notice that they are used for initiating multi-threading. Is it possible that I have unintentionally made my application multi-threading?
Here is the syntax I am using;
Public Delegate Sub EditsMessagerHandlerDelegate(ByRef objThis As IntPtr, _
ByVal strEditTag As String, _
ByVal strEditName As String, _
ByVal strAdminCode As String, _
ByVal strErrorType As String, _
ByVal strMessage As String)
Dim objEditsMessagerHandler As New EditsMessagerHandlerDelegate(AddressOf EditsMessagerHandler)
Dim ptrEditsMessagerHandler As IntPtr = Marshal.GetFunctionPointerForDelegate(objEditsMessagerHandler)
Edit_RunEdits(_intsmfID, _strEditSetTag, _strEditLayoutTag, strNAACCRRecord, _
EE_NOSKIP, intErrCount, ptrThis, ptrEditsMessagerHandler)
My questions;
Will this cause the app to multi-thread?
Would an unintentionally multi-threading app cause the error I am experiencing?
How can I force it to all run in the same thread?
I cannot control the C++ DLL, nor can I see the source.
Edit:
This is not a question about "Object reference not set to an instance of an object." This is a question about whether using p/invoke will inadvertently cause multithreading.
I have read the linked article as well as many others, and none of the issues raised seem to apply. Please do not willy nilly call something a duplicate because you believe the wrong question is being asked. If you have an answer to my specific questions please reply.
I was requested to post a stack trace of the problem, here it is;
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at ACE.OraEditsLib50.processRecord(String strInterRecordEditsName, OracleConnection objConnection) in I:\FCDSApps\OraEditsLib50\OraEditsLib50\OraEditsLib50.vb:line 299
Interestingly enough, line 299 is not the last VB line it executes. I have verified that the fuction call in a different class at that line is executed as well as an additional 10-15 lines of code all the way up until it calls the C++ DLL. EDIT again
While the "duplicate" message above did not solve my problem, the issue my program was having was not related to multi-threading.
I was defining objects and pointers to my delegates in local variables in a method within a class. I was not properly disposing of the objects & pointers before exiting the methods within the class. To resolve the issue I moved those objects to "private" variables as they would never change over the life of the instantiated class, and my problem went away.
One of the other members (I would mention his name but he may not want others to assume he would provide the help he provided to me) here actually looked over some of my code and made the suggestion that fixed the problem.