ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Program crash is occured when I move mouse on the line chart after a while.(VC++)
Posted by Tony on Jul-02-2015 18:36
Hi,

I want to design a line chart and update data quickly by thread.
The program crash is occured when I move mouse on the line chart after a while.

Crashe  information:
"system.accessviolationexception attempted to read or write protected memory"

Break point:
bool makeChart(int format, const char **data, int *len)
{ return CBaseChart_makeChart2(ptr, format, data, len); }

But OnTimer is OK.

How can I solve this issue?


The sample code as below:
//--------------------------------------------------------------------------
static UINT DefThreadProc(LPVOID pParm)
{
     CTestView *pDlg = (CTestView *)pParm;
     while (1)
     {
               pDlg->SpectrumGraph();
     }

return 1;
}

void CSpectraSMARTUtilityView::SpectrumGraph()
{

int gradient[] = {0, 0x2A193C, 13, 0x4C0593, 26, 0x4C05E9};
        AreaLayer *Layers;
        XYChart *c = new XYChart(picX + 20, picY + 20, 0xffffff, 0xffffff);
c->setPlotArea(65, 20, picX - 60, picY - 60, 0xffffff, -1, -1, c->dashLineColor(0xffffff,
0xffffff), c->dashLineColor(0xffffff, 0xffffff));
        int GradientColor = c->linearGradientColor(c->getPlotArea()->getLeftX(), 0,
c->getPlotArea()->getRightX(), 0,IntArray(gradient, (int)(sizeof(gradient) / sizeof(*gradient))));
     c->xAxis()->setLinearScale(340, 850, 30, 6);
     c->yAxis()->setLinearScale(0, 65000, 5000, 1000);

     m_Picture.setChart(c);

}

  Re: Program crash is occured when I move mouse on the line chart after a while.(VC++)
Posted by Peter Kwan on Jul-02-2015 20:20
Hi Tony,

In MFC, your code should only access GUI objects in the thread that creates the GUI
object. In your code, it accesses m_Picture, but it is not sure which thread creates
m_Picture. In your attached code, the DefThreadProc does not seem to create the
m_Picture object. If this is the case, the m_Picture must not be used in your thread. You
can run all the charting code in your thread, except the line using m_Picture.setChart. That
line should be run in the thread that creates the m_Picture.

It is because the m_Picture can receive many events (such as mouse events, timer
events, and hundreds of other windows events). All those events will be delivered using
the thread that creates the m_Picture. If your code tries to modify m_Picture when
m_Picture is processing the events from the other threads, the code may crash.

You mentioned that the program crashes when you move the mouse. The m_Picture
might be processing the mouse events at that time in another thread. It may be trying to
access the chart to see if there are any hot spots that needs pop-up tooltips, and if you
are using track lines, it may be trying to update the chart with the track line. If your code
calls setChart, it will remove the old chart from the m_Picture while it is being updated in
another thread. I am not sure what your code would do with the old chart. In our sample
code, we simply delete it to avoid memory leak. This means the chart is deleted in your
thread while it is being processed, and this is one method that can crash the code.

Another possibility for the crash is invalid parameters. It is not sure what is picX and picY in
your code. If it is a small value, the picY - 60 could become negative, which is invalid. For
testing, it is best to always use hard coded data.

Regards
Peter Kwan