CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    [RESOLVED] atlsimpstr.h access violation error

    hi,
    Recently, i got an "atlsimpstr.h access violation reading location" error when run my project.

    I'm using two threads in my project. If i run single thread the above error did not come.

    Code:
    UINT MainSequence(LPVOID pVoid)
    {
    	HWND hwnd = (HWND)pVoid;
    	CMainDlg *Main = CMainDlg *)pVoid; 			
    
    	while(1)
    	{
    	  // do some process
    	}
    return 0;
    }
    
    UINT SignalSequence(LPVOID pVoid)
    {
    	HWND hwnd = (HWND)pVoid;
    	CMainDlg *Main = CMainDlg *)pVoid; 
    	while(1)
    	{
    	  // do some process
    	}
    return 0;
    }
    
    
    CWinThread *tMain;
    CWinThread *tSignal;
    BOOL CMainDlg::OnInitDialog()
    {
       CDialog::OnInitDialog();
       tMain   = AfxBeginThread(MainSequence, LPVOID(NULL));
       tSignal = AfxBeginThread(SignalSequence, LPVOID(NULL));
    
       return TRUE;
    }
    If i command tMain thread , no error occurs.
    How can i handle these two threads?
    Regards,

    SaraswathiSrinath

  2. #2
    Join Date
    Aug 2006
    Posts
    231

    Re: atlsimpstr.h access violation error

    It seems like you've missed the left parenthesis when casting CMainDlg. But other than that, it looks like it should work...

    "atlsimpstr.h" indicates that a string is used somewhere. Did you leave out any code that is running inside the while loops?

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: atlsimpstr.h access violation error

    Within mainSequence() and signalSequence() the parameter pVoid is being passed as NULL - which is then being cast to both hwnd and *Main?? You don't show the contents of the infinite while loops so we don't know how hwnd and Main are used - but any attempt to deference Main will produce an error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: atlsimpstr.h access violation error

    Quote Originally Posted by TubularX View Post
    It seems like you've missed the left parenthesis when casting

    CMainDlg. But other than that, it looks like it should work...
    Typing mistake. In my project, correctly declared by
    Code:
    CMainDlg *Main = (CMainDlg *)pVoid;
    Quote Originally Posted by 2kaud View Post
    Within mainSequence() and signalSequence() the parameter pVoid is

    being passed as NULL - which is then being cast to both hwnd and *Main?? You

    don't show the contents of the infinite while loops so we don't know how hwnd and Main are

    used - but any attempt to deference Main will produce an error.
    Code:
    UINT MainSequence(LPVOID pVoid)
    {
    	HWND hwnd = (HWND)pVoid;
    	CMainDlg *Main = (CMainDlg *)pVoid; 
    	while(1)
    	{
    	    if(BTN1_PRESSED == 1)
                {
                      Main->StatusEnbl("BTN1 Processing");
                      Main->Process1();
                      Main->InsertLog("1");
                      Main->StatusDsbl("");
                }   
    
    	    if(BTN2_PRESSED == 1)
                {
                      Main->StatusEnbl("BTN2 Processing");
                      Main->Process2();
                      Main->InsertLog("2");
                      Main->StatusDsbl("");
                }           //up to 3,4, .... 20 Process  
     
                if(BTN20_PRESSED == 1)
                {
                      Main->StatusEnbl("BTN20 Processing");
                      Main->Process20();
                      Main->InsertLog("20");
                      Main->StatusDsbl("");
                }                
    	}
              return 0;
    }
    Code:
    UINT SignalSequence(LPVOID pVoid)
    {
    	HWND hwnd = (HWND)pVoid;
    	CMainDlg *Main = (CMainDlg *)pVoid; 
    	while(1)
    	{
    	    if(SIGBTN1_PRESSED == 1)//bool SIGBTN1_PRESSED;
                {
                      Main->StatusEnbl("SIGBTN1 Processing");
                      Main->SignalProcess1();
                      Main->InsertLog("21");
                      Main->StatusDsbl("");
                }   
    
    	    if(SIGBTN2_PRESSED == 1)
                {
                      Main->StatusEnbl("SIGBTN2 Processing");
                      Main->SignalProcess2();
                      Main->InsertLog("22");
                      Main->StatusDsbl("");
                }   
    	}
    return 0;
    }
    Code:
    void CMainDlg::SignalProcess1()
    {
            //InitPort(2, 9600, 'N', 8, 1)
    	//Serial communication Write Request & Read Response.	
    }
    
    //Note SignalProcess2() using the same port like SignalProcess1() & doing serial processing
    Code:
    void CMainDlg::Process1()
    {
            //InitPort(3, 9600, 'N', 8, 1)
    	//Serial communication Write Request & Read Response.
    }
    
    //Note Process2() to Process20() using the same port like Process1() & doing serial processing
    Regards,

    SaraswathiSrinath

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: atlsimpstr.h access violation error

    Code:
    tMain   = AfxBeginThread(MainSequence, LPVOID(NULL));
    ...
    INT MainSequence(LPVOID pVoid)
    {
    	HWND hwnd = (HWND)pVoid;
    	CMainDlg *Main = (CMainDlg *)pVoid; 
    ...
    Main->StatusEnbl("BTN1 Processing");
    in MainSequence (and SignalSequence), pVoid is NULL, so Main is NULL and de-referencing a NULL pointer will generate an error.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: atlsimpstr.h access violation error

    Quote Originally Posted by 2kaud View Post
    in MainSequence (and SignalSequence), pVoid is NULL, so Main is NULL and de-referencing a NULL pointer will generate an error.
    ok.

    I have a doubt, today i received below errors from atlsimpstr.h,

    1. nLength <=GetData() -> AllocLength

    2. nrefs != 0

    3. CStringData* pNewData = pOldData->Clone->Allocate(nLength, sizeof(XCHAR));
    if(pNewData == NULL ) ThrowMemoryException


    I refered https://social.msdn.microsoft.com/fo...dialog-pointer.
    From this link, i have a doubt from string handling.
    I'm guessing the assert has something to do with some CString manipulation you're doing probably in the destructor of the dialog class.
    and confirmed using call stack.
    To find out the variable that holds the culprit CStringData, you can click “Retry” when you see the assert dialog. This will start up your JIT debugger (e.g. Visual Studio or windbg) to attach to the process. After the debugger is attached, open the call-stack window. (In Visual Studio, the call-stack window can be opened in Debug menu ->
    Windows -> Call Stack).
    Is the problem is nonproper handling of String?
    Regards,

    SaraswathiSrinath

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: atlsimpstr.h access violation error

    Quote Originally Posted by saraswathisrinath View Post
    ok.

    I have a doubt, today i received below errors from atlsimpstr.h,

    1. nLength <=GetData() -> AllocLength

    2. nrefs != 0

    3. CStringData* pNewData = pOldData->Clone->Allocate(nLength, sizeof(XCHAR));
    if(pNewData == NULL ) ThrowMemoryException


    I refered https://social.msdn.microsoft.com/fo...dialog-pointer.
    From this link, i have a doubt from string handling.

    and confirmed using call stack.


    Is the problem is nonproper handling of String?
    A: 99.999% yes, is a problem of improperly handling of CString
    You can simply reproduce this behavior writing the following simple code:
    Code:
        CString* pString = new CString(_T("Geo luvs strings"));
        delete pString;
        // ...
        delete pString;
    However, why the assert complains about nRefs?
    An instance of a CString object has before its string buffer a CStringData structure.
    Beside others, CStringData structure has a member named nRefs which is almost like a reference counter of a shared smart pointer. It avoids to have more than one CString object with different buffers having the same string contents. When assign one CString object to another new one, the new one gets the same string buffer and just CStringData::nRefs is incremented. Practically we can have more CString objects but in fact, only one string buffer in memory.
    When delete one of "copies", CStringData::nRefs is decremented, and the string buffer is effectively deleted from memory only if CStringData::nRefs becomes 0 (zero).
    Last edited by ovidiucucu; March 27th, 2015 at 03:09 PM. Reason: typo
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    Re: atlsimpstr.h access violation error

    Thank you Mr.ovidiucucu. I was used temporary CString variables, having incoming serial data and stored in a file for verify the data is correct or not. This process running under MainSequence thread. that why running good when MainSequence thread is in rest condition. Just i commands that codes and ran my project, its working with out any error.
    Regards,

    SaraswathiSrinath

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured