[vtkusers] the vtkDoubleArray cause segmentation fault
张驭洲
yzhzhang at ipe.ac.cn
Tue May 9 08:36:09 EDT 2017
Hello,
I have a code that uses VTK, but it can't run normally. As I have tried to solve the problem for two days but in vain and there is no one around me that familiar with VTK, I come here to look for help. Here is a piece of my code:
vtkUnstructuredGrid* VTKGrid;
template <typename T3, typename T4, typename T5>
void UpdateVTKAttributes(T3& U_Field, T4& p_Field, T5& T_Field)
{
if(VTKGrid->GetCellData()->GetArray("p") == NULL)
{
// pressure array
vtkNew<vtkDoubleArray> p;
p->SetName("p");
p->SetNumberOfComponents(1);
p->SetNumberOfTuples(static_cast<vtkIdType>(p_Field.size()));
VTKGrid->GetCellData()->AddArray(p.GetPointer());
}
vtkDoubleArray* p = vtkDoubleArray::SafeDownCast(
VTKGrid->GetPointData()->GetArray("p"));
vtkIdType numTuples = p->GetNumberOfTuples();
int temp[400];
for(vtkIdType i=0; i<numTuples; i++)
{
//cout << p_Field[i] << " ";
//printf("%.15lf ", p_Field[i]);
//if((i+1) % 10 == 0)
//printf("\n"); //these are OK
//temp[i] = p_Field[i];
//p->SetTuple1(i, temp[i]); // fault
//printf("%lf ", p->GetTuple1(i)); //fault
//if((i+1) % 10 == 0)
//printf("\n");
//p->InsertTuple1(i, p_Field[i]); //fault
//p->InsertNextTuple1(p_Field[i]); //fault
p->SetTuple1(i, p_Field[i]);
}
}
When I run the executable file, I get segmentation fault:
#0 Foam::error::printStack(Foam::Ostream&) in "/opt/OpenFOAM/OpenFOAM-2.3.x/platforms/linux64IccDPOpt/lib/libOpenFOAM.so"
#1 Foam::sigSegv::sigHandler(int) in "/opt/OpenFOAM/OpenFOAM-2.3.x/platforms/linux64IccDPOpt/lib/libOpenFOAM.so"
#2 ? in "/lib64/libpthread.so.0"
#3 vtkDataArray::SetTuple1(long long, double) in "/pan20/yzzhang/paraview-5.2.0-release/lib/libvtkCommonCore-pv5.2.so.1"
#4 void Adaptor::CoProcess<Foam::DynamicList<float, 0u, 2u, 1u>, Foam::DynamicList<int, 0u, 2u, 1u>, Foam::DynamicList<float, 0u, 2u, 1u>, Foam::DynamicList<float, 0u, 2u, 1u>, Foam::DynamicList<float, 0u, 2u, 1u> >(Foam::DynamicList<float, 0u, 2u, 1u>&, Foam::DynamicList<int, 0u, 2u, 1u>&, Foam::DynamicList<float, 0u, 2u, 1u>&, Foam::DynamicList<float, 0u, 2u, 1u>&, Foam::DynamicList<float, 0u, 2u, 1u>&, unsigned int, bool) at ~/Desktop/myfoam/adaptor.h:90
#5 main at ~/Desktop/myfoam/runCatalyst.C:288
#6 __libc_start_main in "/lib64/libc.so.6"
#7 ? in "/root/Desktop/myfoam/OFcatalyst"
Segmentation fault (core dumped)
I'm think it is the statement "p->SetTuple1(i, p_Field[i]);" that causes the fault, and any other vtk function that references array p also causes the same fault. And, the same fault appears even without use of for loop. Just a single statement " p->SetTuple1(0, p_Field[0]);" causes the fault. However, if I only print the p_Field without any reference of vtkDoubleArray p, the program runs with no fault. So I guess maybe the memory of array p was no allocated properly? I'm not sure.
Could you please tell me where the error is in that code? Thanks a lot!
sincerely
-Zhang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170509/b2df259b/attachment.html>
More information about the vtkusers
mailing list