I have an issue with an MFC dialog based application build with MSVC 2013. To make the main dialog accessible also during more elaborate functions, I'm using multi-threading. A click on a button in the dialog calls a "worker function" that is worked out by another thread.
Here's an excerpt of the class:
class CpiezcamDlg : public CDialogEx
{
protected:
virtual BOOL OnInitDialog();
public:
CWinThread *m_thread1;
void StartSweepAndImageThread()
{
m_thread1 = AfxBeginThread(SweepAndImageThreadProc, this);
}
private:
static UINT SweepAndImageThreadProc(LPVOID pParam)
{
CpiezcamDlg *pThis = (CpiezcamDlg *)pParam;
UINT nRet = pThis->DoSweepAndImage();
return nRet;
}
UINT DoSweepAndImage();
UINT16 steps;
CString *imgs_name;
};
Clicking a button calls StartSweepAndImageThread
which itself calls SweepAndImageThreadProc
and finally DoSweepAndImage
. In the function DoSweepAndImage
, variables of the class are accessed (read and write). Amongst others, there is imgs_name
. The usage is:
UINT CpiezcamDlg::DoSweepAndImage()
{
// ...
CString str;
str.Format(_T("Test"));
AddStageListText(str);
imgs_name[i] = str;
// ...
}
while imgs_name
is initialized like
steps = 4;
imgs_name = new CString[steps];
in the OnInitDialog
function.
The problem is that when pressing the mention button I receive
0xC0000005: Access violation reading location 0xFDFDFDF9.
exactly on imgs_name[i] = str;
. When using a statical array, that is instead of CString *imgs_name;
I define CString imgs_name[4];
, everything works well. However, I very much would like to have that CString
variable a dynamical one. Thanks in advance for your help!
PS: When I evaluated this in a serial way, i.e. when running the DoSweepAndImage
function in the main thread, everything goes well. That's why I assume the access violation is due to the multi-threading.
@Wimmel: The loop over i
in DoSweepAndImage
is
for (UINT16 i = 0; i < steps; i++)