locked
Heap corruption and IBM MQ amqpdnet.dll RRS feed

  • Question

  • I have been working on an application that references a properly installed IBM MQ client (8.0.0.1) and interacts with a queue. On any given run, either the application when run through the vshost with or without debugging as well as being run without vshost through the command line throws a type initialization error. Usually when one does, running it through one of the remaining two ways works?

    This is such a common occurrence for this library its not even funny. I am however using an installed client which is a common cause. The fact that this is sporadic between compilations is mind boggling. For the most part everything was working fine.

    Moved the binary to a client and another issue manifested where an assembly with some unmanaged code now throws an exception at application exit. Ran that through a debugger to see it bailed on possible heap corruption. Used gflags to enable page heap for the binary and it wont start as the IBM MQ assembly throws a type initialization error? So disabling and enabling gflags reliably causes the IBM assembly to fail or succeed indicating the heap corruption possibly happens before I even make the call to my library with unmanaged code.

    So I enabled fusion logging and ran the program, however I am not really sure how to interpret the logs? They look right, they just encounter an exception at initialization?

    How does one troubleshoot this kind of thing?
    Thanks!
    Wednesday, March 25, 2015 9:51 PM

Answers

  • So, I removed all the unmanaged code and it ran fine with page heap enabled, started adding it back and only one class with non critical usage would cause it reliably to fault when page heap was enabled.

    So, either that c dll, or the IBM libs do something bad. I had a colleague who does low level development take a look with windbg but as it fails outside of a call to my unmanaged code and within an IBM call it wasn't straight forward.

    I just removed the small class and worked around it.

    Thanks for all the effort Joel!
    • Marked as answer by Caillen Wednesday, April 1, 2015 8:42 AM
    Thursday, March 26, 2015 9:27 PM

All replies

  • Do one of the following with any exception

    1) In exception handler output the e.StackTrace

    2) You can also get Stack Trace if an exception pop up occurs where it says "Copy to Clipboard".  Press this shortcut and then paste into Notepad.


    jdweng

    Wednesday, March 25, 2015 10:11 PM
  • Hi Joel,

    The exception is:
    Unhandled Exception: System.TypeInitializationException: The type initializer for 'IBM.WMQ.MQQueueManager' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Failed to load mqe.dll from folder C:\Program Files\IBM\WebSphere MQ\bin\
       at IBM.WMQ.Nmqi.NativeManager.InitializeNativeApis(String mode)
       --- End of inner exception stack trace ---
       at IBM.WMQ.MQCommonServices..ctor()
       --- End of inner exception stack trace ---
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
       at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
       at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
       at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       at IBM.WMQ.CommonServices.CreateCommonServices()
       at IBM.WMQ.CommonServices.TraceConstructor(String objectId, String sccsid)
       at IBM.WMQ.Nmqi.NmqiEnvironment..ctor(NmqiPropertyHandler nmqiPropertyHandler)
       at IBM.WMQ.Nmqi.NmqiFactory.GetInstance(NmqiPropertyHandler properties)
       at IBM.WMQ.MQQueueManager..cctor()
       --- End of inner exception stack trace ---
       at IBM.WMQ.MQQueueManager..ctor(String queueManagerName, String Channel, String ConnName)
       at Company.MyApplication.WebSphereMQ.MQConnection.Connect() in c:\scm\MyApplication\MQConnection\MQConnection.cs:line 158
       at Company.MyApplication.Library.MyApplicationLibrary.Scheduler(CancellationTokenSource cancellationTokenSource) in c:\scm\MyApplication\MyApplicationLibrary\ClassFile.cs:line 1513
       at Company.MyApplication.Library.MyApplicationLibrary.<>c__DisplayClass55.<SchedulerControl>b__53() in c:\scm\MyApplication\MyApplicationLibrary\ClassFile.cs:line 1552
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
    
    Thanks!
    Wednesday, March 25, 2015 10:40 PM
  • You are failing on line below.  You are not completing the connection to the database.  The database could be corrupted, not exist (wrong location), or one of the parameters is wrong.  You also may not have the correct credentials to access the database.

       at IBM.WMQ.MQQueueManager..ctor(String queueManagerName, String Channel, String ConnName)

    The line causing the error is here

    at Company.MyApplication.WebSphereMQ.MQConnection.Connect() in c:\scm\MyApplication\MQConnection\MQConnection.cs:line 158


    jdweng

    Wednesday, March 25, 2015 10:50 PM
  • VS and the Net Library are managed to prevent Blue Screen Exceptions.  The VS compile inserts an exception handler into every application before calling main() to handle the "Unhandled Exceptions".   Code is also put into every methods to manage exceptions.  When an exception occurs the added code moves up the execution stack and finds the first exception handler (try/catch) and reports the error.  If no exception handler is found.  It is recommended to add exception handlers to code to accurately report when an exception occurs.  When exception handlers are missing it can be confusing because the exception is reported in a different place in the code.

    jdweng

    Wednesday, March 25, 2015 11:37 PM
  • Hi Joel,

    The thing is happens across multiple similar dev environments when connecting to a queue. Security is host based so all that is passed are string values to the initializer.

    I do understand all that you have said, but the type of exception and message (Failed to load mqe.dll) sure sounds more like an assembly binding issue, no? I can throw some asserts in there to check for empty strings but I am pretty certain this library has some other issues, whether or not its caused by some unmanaged code I am using or not.

    We have replicated this across different versions in multiple environments.

    Thanks a lot Joel.
    Thursday, March 26, 2015 12:29 AM
  • To bind, you first have to connect.  You can't connect to a null object.  If exception handlers aren't placed in correct location or objects aren't tested for nulls misleading error messages can occur.

    jdweng

    Thursday, March 26, 2015 1:02 AM
  • Ahh,
    I think that is where things went astray. We aren't failing to bind to a service or some connection, but its failing to load the assembly itself. I believe its an assembly bind failure, not an authentication binding error. When enabled, the fusion logs (https://msdn.microsoft.com/en-us/library/e74a18c4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) do show an exception occurred when loading the type itself. Very odd...

    Thanks!
    Thursday, March 26, 2015 1:31 AM
  • See IBM slide presentation below

    http://www-01.ibm.com/support/docview.wss?uid=swg27024340&aid=1


    jdweng

    Thursday, March 26, 2015 6:59 AM
  • Hi Joel,
    That confirms my installation as per the criteria with versions, installed libs and assemblies registered.

    When the type fails to load, the fusion log indicates the following:

    *** Assembly Binder Log Entry  (3/25/2015 @ 4:41:11 AM) ***
    The operation was successful.
    Bind result: hr = 0x1. Incorrect function.
    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\path\binary_name.exe

    --- A detailed error log follows. 
    BEGIN : Native image bind.
    END   : Incorrect function. (Exception from HRESULT: 0x00000001 (S_FALSE))

    Not really sure that provides much more insight...

    Thanks for all the help!
    Thursday, March 26, 2015 3:31 PM
  • The line below is the last line to get executed.  Still think it is a connection issue.  The line below looks like a low level connection.  I would look at the IBM link I provided.  There is some good info that may help.  The binding error is just a result of the connection failing, not the root cause.  No dataq is getting returned so you don't have anything to bind to.  It is possible the wrong type data is being returned, but I don't think that is the case.

       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)


    jdweng

    Thursday, March 26, 2015 3:51 PM
  • Maybe you are using the wrong "String queueManagerName".   I'm assuming this is the "Incorrect function" in the error log.

    jdweng

    Thursday, March 26, 2015 6:05 PM
  • Ok, I threw some log statements directly above the call to that classes ctor and logged the input.

    And coincidentally, after rebuilding: Ctrl-F5 throws a TypeInitializationException, whereas F5 runs as expected. The input is all valid for each case when it faults or runs properly.

    Truth be, the class the provides the configuration makes assertions with the values at least being populated and not null or white space.

    I have some unmanaged code in two classes. I have thought about trying to remove it to see if it is the cause of the heap corruption and possibly the IBM libs just randomly bail when it happens to affect them. One class could be removed easy, the other is next to impossible but I could.

    When the app *will* run, it seems with page heap enabled it faults instantly. Disabling page heap allows it to function again. I'll go through the pain to see if the unmanaged code is the root by stubbing out the calls manually.

    Again, thanks for the continued effort.
    Thursday, March 26, 2015 8:00 PM
  • So, I removed all the unmanaged code and it ran fine with page heap enabled, started adding it back and only one class with non critical usage would cause it reliably to fault when page heap was enabled.

    So, either that c dll, or the IBM libs do something bad. I had a colleague who does low level development take a look with windbg but as it fails outside of a call to my unmanaged code and within an IBM call it wasn't straight forward.

    I just removed the small class and worked around it.

    Thanks for all the effort Joel!
    • Marked as answer by Caillen Wednesday, April 1, 2015 8:42 AM
    Thursday, March 26, 2015 9:27 PM