How to solve the Line plot error?

I’m referring to an example of a line plot, and if the button is pressed with MFC, it’s printed on picture control.

It works normally when the button is pressed for the first time, but if you plot it again, an error will appear. How should I handle this?

my code is here:

vtkNew table;

vtkNew<vtkFloatArray> arrX;
arrX->SetName("X Axis");
table->AddColumn(arrX);

vtkNew<vtkFloatArray> arrC;
arrC->SetName("Line Profile");
table->AddColumn(arrC);


// Fill in the table with some example values
int numPoints = 550;
float inc = 1;//7.5 / (numPoints - 1);
table->SetNumberOfRows(numPoints);
for (int i = 0; i < numPoints; ++i)
{
	table->SetValue(i, 0, i * inc);
	table->SetValue(i, 1, meanMat[i]);//cos(i * inc));
}

// Set up the view
vtkNew<vtkContextView> view;
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);

// 추가
view->SetRenderWindow(m_vtkRenderWindow);

// Add multiple line plots, setting the colors etc
vtkNew<vtkChartXY> chart;
view->GetScene()->AddItem(chart);
vtkPlot *line = chart->AddPlot(vtkChart::LINE);
line->SetInputData(table, 0, 1);
line->SetColor(255, 0, 0, 255);
line->SetWidth(3.0);


// For dotted line, the line type can be from 2 to 5 for different dash/dot
// patterns (see enum in vtkPen containing DASH_LINE, value 2):

#ifndef WIN32
line->GetPen()->SetLineType(vtkPen::DASH_LINE);
#endif
// (ifdef-ed out on Windows because DASH_LINE does not work on Windows
// machines with built-in Intel HD graphics card…)

//view->GetRenderWindow()->SetMultiSamples(0);

// Start interactor
view->GetRenderWindow()->Render();
view->GetInteractor()->Initialize();

Hello,

Can you post the error message?

regards,

Paulo

Hello, Paulo.

Thank you, reply the message.

My error message is that…

Unhandled exception in “…exe”: 0x00007FFD650A6259(vtkViewsContext2D-8.90d.dll) Access violation reading location 0xC0000005: 0xFFFFFFFFFFFFFFFF.

i don’t know why the displaying the message. I just hope that the redrawing a chart.

Hi,

Can you post the stack trace whan the crash ensues, please?

regards,

Paulo

Hi,

My program’s stack trace window is that

Thank you, Paulo.

Triple check you are not mixing debug and non-debug libraries/binaries. Mixing them is not good and often leads the program to do seemingly unexplainable behavior or crashes (such as crashing after a return instruction).

Thank you, Paulo.

I found this problem.
After initializing the chart, the render was resolved.

Thank you for spending a lot of time.

Hello,

Can you, please, post the code to initualize the chart so others with the same issue can apply your solution?

thanks,

Paulo