[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?
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?
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?? :confused: 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.
Re: atlsimpstr.h access violation error
Quote:
Originally Posted by
TubularX
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
Within mainSequence() and signalSequence() the parameter pVoid is
being passed as NULL - which is then being cast to both hwnd and *Main?? :confused: 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
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.
Re: atlsimpstr.h access violation error
Quote:
Originally Posted by
2kaud
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.
Quote:
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.
Quote:
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?
Re: atlsimpstr.h access violation error
Quote:
Originally Posted by
saraswathisrinath
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).
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.