Quantcast

the vtkDoubleArray cause segmentation fault

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

the vtkDoubleArray cause segmentation fault

张驭洲


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());
                }  
    &nb sp;           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

             &nbs p;          //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]);
  &nb sp;             }
    }

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&g t;&, 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




_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: the vtkDoubleArray cause segmentation fault

Cory Quammen-2
You are adding the "p" array to VTKGrid's cell data, but then
accessing "p" from VTKGrid's point data, which is probably NULL. Make
these match and you will likely have better luck.

- Cory

On Tue, May 9, 2017 at 8:36 AM, 张驭洲 <[hidden email]> wrote:

>
>
> 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());
>                 }
>     &nb sp;           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] << " ";
>        &nbsp ;                //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
>
>              &nbs p;          //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]);
>   &nb sp;             }
>     }
>
> 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&g t;&, 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
>
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>



--
Cory Quammen
Staff R&D Engineer
Kitware, Inc.
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers
Loading...