none
Passing unmanaged array to a Managed Thread. - memory corruption RRS feed

  • Question

  • I'm a newbie for .Net and trying to use managed threading.
    I couldn't find any problem in my code, but it triggers an exception when the Thread Ends.
    Something like:
    Unhandled exception at 0x5cbf80ea (msvcr90d.dll)
    0xC0000005: Access violation reading location 0x000000d7.
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::Threading;
    
    #define sz 100
    
    //int dt[sz]; //allcating a gloal buffer
    int *dt;
    
    
    void workerThread (void)
    {
    	Console::WriteLine("Producer Thread Started!");
    	int data = 50;
    	for(int i=0; i<sz; i++)
    	{
    		Thread::Sleep(1);
    		dt[i] = i;
    		Console::WriteLine("Producer W={0}", i);
    	};
    	Console::WriteLine("Producer Thread Ending");
    }
    
    int main(array<System::String ^> ^args)
    {
      Console::WriteLine("This is a test on global variable and threading");
    	//allcating a buffer
    	dt = new int(sz);
    
    	Thread ^wthrd = gcnew Thread(gcnew ThreadStart(&workerThread));
    	//Starting Worker Thread..
    	wthrd->Start(); 
    	//Waiting for Worker Thread to end.
    	wthrd->Join();	
    	Console::WriteLine("Worker Thread Ended.");
    	Console::ReadKey();
      return 0;
    }
    

    However it works fine when I allocate the buffer as a global array. This exception kicks in when I use "new" keyword, hence a  dynamic memory allocation.
    Am I making any fundamental mistakes?
    Is this something to deal with the garbage collector? or Unmanaged heap allocated by the "new" keyword?
    I would really prefer to have this buffer in unmanaged heap. Although I am writing a managed code, many other DLLs I am using are unmanaged.
    • Changed type eryang Monday, November 29, 2010 1:56 AM it is a question
    Saturday, November 27, 2010 5:41 PM

Answers

  • Thank you very much for your effort.   However this issue was quite elementary and resolved.

    dt = new int(sz);
    

    is not right and what we need is:

    dt = new int[sz];
    

    http://stackoverflow.com/questions/4293117/passing-unmanaged-array-to-a-managed-thread-memory-corruption

    Nevertheless the debugger doesn't complain about the mistake until the thread tries to end. VS debugger is not aware of these kind of situations? I'm surprised that the debugger is not handling this well. I'm quite interested to find out why it is not handling this in a straightforward way?


    • Proposed as answer by MarkRockmann Wednesday, December 1, 2010 7:38 PM
    • Marked as answer by eryang Monday, December 27, 2010 1:00 PM
    Monday, November 29, 2010 1:07 PM

All replies

  •  

    Hi,

     

    Thanks for your code snippet, I can reproduce the Access Violation exception. Another thing I found is: your code snippet works well if I run the code without debugger attached (Ctrl+F5), the exception only occurs when I run the code under VS Debugger (F5), the call stack shows that the exception may related to debugger, we're doing research on this issue. It might take some time before we get back to you.

     

             _woutput_s_l(stream, format, plocinfo, argptr) + 0xf2a bytes        C++

             _vswprintf_helper(woutfn, string, count, format, plocinfo, ap) + 0x146 bytes        C

             _vsnwprintf_s_l(string, sizeInWords, count, format, plocinfo, ap) + 0x20a bytes        C

             _snwprintf_s(string, sizeInWords, count, format, ...) + 0x27 bytes        C

             _VCrtDbgReportW(nRptType, szFile, nLine, szModule, szFormat, arglist) + 0x3e3 bytes        C

             _CrtDbgReportWV(nRptType, szFile, nLine, szModule, szFormat, arglist) + 0x22 bytes        C++

             _CrtDbgReportW(nRptType, szFile, nLine, szModule, szFormat, ...) + 0x2b bytes        C++

             _free_dbg_nolock(pUserData, nBlockUse) + 0x139 bytes        C++

            _free_dbg(pUserData, nBlockUse) + 0x50 bytes        C++

             _freefls(data) + 0x59 bytes        C

             RtlProcessFlsData(FlsData) + 0xa66 bytes        C

             LdrShutdownThread() + 0x35 bytes        C

             RtlExitUserThread(ExitStatus) + 0x2a bytes        C

             BaseThreadInitThunk(RunProcessInit, StartAddress, Argument) + 0x19 bytes        C

             __RtlUserThreadStart(StartAddress, Argument) + 0x27 bytes        C

             _RtlUserThreadStart(StartAddress, Argument) + 0x1b bytes        C


    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.
    Monday, November 29, 2010 4:09 AM
  • Thank you very much for your effort.   However this issue was quite elementary and resolved.

    dt = new int(sz);
    

    is not right and what we need is:

    dt = new int[sz];
    

    http://stackoverflow.com/questions/4293117/passing-unmanaged-array-to-a-managed-thread-memory-corruption

    Nevertheless the debugger doesn't complain about the mistake until the thread tries to end. VS debugger is not aware of these kind of situations? I'm surprised that the debugger is not handling this well. I'm quite interested to find out why it is not handling this in a straightforward way?


    • Proposed as answer by MarkRockmann Wednesday, December 1, 2010 7:38 PM
    • Marked as answer by eryang Monday, December 27, 2010 1:00 PM
    Monday, November 29, 2010 1:07 PM