none
" Access violation" Exception on exiting application (unmanaged manged code) on Windows Vista and 7 in release mode in Minternal.h RRS feed

  • Question

  • Hi  ,

    I  have a  managed wapper  library that I am calling from  a c# application developed on 2008.  The application works fine on xp. Works in debug mode  on win7 and Vista .

    In release mode  It works  fine through happy path until I exist the application or try to reload the library is throws "Memory access violation" in  SingletonDomainUnload function in Minternal.h  with description-  "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".

    I am noice in  creating/managing  unmanaged-managed code  - I would appriciate  help/pointers  on   how to go about debugging this issue itself.

    Thanks in advance,

    Megha

     

     

    Thursday, April 15, 2010 7:03 PM

All replies

  • Hi,

    A quick question, what do you meaning "reload the library "?

     

    From my understanding, your application hierarchy may looks like:

    C# client --> managed wrapper dll --> COM Server

    Am I right?

     

    Since managed objects are automatically reclaimed byGarbage Collect (GC) , meanwhile, the exception always occurs when C# client exit, it sounds to me that the COM Server cached managed object, once C# application exit, the managed object get destroyed, but COM Server keeps the object handle and tries to do something on it.

    For example, in C# client, if you register a callback to COM server:

     

    comObjWrapper.RegisterCallBack(managedCallbackDelegate);

     

    You may need to unregister the callback before C# client exit:

     

    comObjWrapper.UnRegisterCallBack(managedCallbackDelegate);


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, April 16, 2010 2:45 AM
  • Hi Megha,

    How about the issue status now?


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 20, 2010 12:35 AM
  • Hi ,

    Thanks for your response.  The code doesn't seem to be using both of them.

    Here is LoadFunction which is  blackbox to me ,it works on (XP release mode) so I am wondering if  the  way its being invoked/ any settings would make the difference.

    I added  another line to free memory  for last file which was missing seemed to have no difference.

    System::Runtime::InteropServices::Marshal::FreeHGloba(softwareConfigPath);

    Thanks,

    Megha

    static int LoadOptimizerFromConfigs(
    System::String ^userConfigPath,
    System::String ^cobeConfigPath,
    System::String ^softwareConfigPath)
    {
    System::IntPtr userConfigFileP = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(userConfigPath);
    System::IntPtr cobeConfigFileP = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(cobeConfigPath);
    System::IntPtr softConfigFileP = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(softwareConfigPath);
    int results = PredictInstance::GetInstance().LoadOptimizerFromConfig(
    (char*)userConfigFileP.ToPointer(), 
    (char*)cobeConfigFileP.ToPointer(),
    (char*)softConfigFileP.ToPointer());
    System::Runtime::InteropServices::Marshal::FreeHGlobal(userConfigFileP);
    System::Runtime::InteropServices::Marshal::FreeHGlobal(cobeConfigFileP);
    return results;
    }

     

     

     

     

    Monday, April 26, 2010 3:21 PM
  •  

    Is the exception thrown in this line?

     

    int results = PredictInstance::GetInstance().LoadOptimizerFromConfig(...);

     

    If method PredictInstance::GetInstance().LoadOptimizerFromConfig is available for you, you may step into this method to see what object brings this exception.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 27, 2010 11:31 AM
  • Hi Eric ,

    The exception happens because of the above method but not inside it. It also does not happen in debug mode.

    I get the exception  when I exit  the application  (after form closing) if  during the excution of teh application  LoadOptimizerFromConfig() was called once .

    If I call above method twice and exit the application  somehow the problem gets  fixed.

    The exception goes away if I remove the  global exception handler code   (which is set to catch unexpected scenarios on customer site which writes to log and can be emailed back for troubleshooting.)

    I don't understand  why is global handler catching this exception  in release mode (only on vista and 7) but os doesn't throw it otherwise?

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(HandleException);
    
     static void HandleException(object sender, UnhandledExceptionEventArgs args)
        {
          Exception ex = (Exception)args.ExceptionObject;
          log.ErrorFormat("internal_unknown_error with reason :{1}", ex.InnerException);
          MessageBox.Show(EMEAControlLocalizer.LocalizeString("internal_unknown_error"),
            EMEAContextExpert.ApplicationName,
                  MessageBoxButtons.OK,
                  MessageBoxIcon.Error);
          FileIO.DeleteDirectory(EMEAContextExpert.PredictionReportTempDirectory);
          Environment.Exit(0);
    
    
        }

    Thanks,

    Megha

    Monday, May 3, 2010 3:25 PM
  •  

    Seems that some native methods still try to access managed resources after appdomain was unloaded. You may use WinDbg to find out which native method throws the Access Violation exception:

    1. Launch your application, in WinDbg form, press F6 to attach to the application process.

    2. In WinDbg, click Debug menu --> Event Filters, on the popup dialog, make sure Access Violation is enabled.

    3. In WinDbg, input command 'g' to let your application running.

    4. Exit your application, WinDbg will take control once the application throw Access Violation exception.

    5. Then, input command 'k' to see the current call stack, so that you can see which method brings the exception.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, May 4, 2010 7:22 AM