[vtkusers] vtkInformation access with equivalent keys

Alexander Pletzer pletzer at txcorp.com
Wed Mar 7 18:00:22 EST 2012


Hi,

The following small code shows how to store and access information by 
key. The code runs fine but if I comment out line

// double *vals = info->Get(key2); // this throws a seg fault....

then I get a segmentation fault. Here, key2 has the same values as key 
and therefore I would expect key2 can also be used to access the 
information. I imagine info to be attached to a vtkDataSet object and 
key2 to be created in a different location than key, I will not have 
access to key where I call info->Get. Thus, I need a mechanism for 
rebuilding a key from name/location and get back the information attched 
to this doublet.

Thanks in advance for any help.

--Alex


// vtk includes
#include <vtkDoubleArray.h>
#include <vtkInformation.h>
#include <vtkInformationDoubleVectorKey.h>

// std includes
#include <iostream>

int main() {

   // create a 2-element array
   vtkDoubleArray* array = vtkDoubleArray::New();
   array->SetName("array");
   array->SetNumberOfComponents(1);
   array->SetNumberOfTuples(2);
   array->SetValue(0, 1.);
   array->SetValue(1, 2.);

   // access the info (presently none stored)
   vtkInformation* info = array->GetInformation();

   // add one attribute, a double vector
   const char *name = "myKey";
   const char *location = "MyClass"; //
   const int length = 3;
   vtkInformationDoubleVectorKey *key = new 
vtkInformationDoubleVectorKey(name,
                                                                          location,
                                                                          length);
   vtkInformationDoubleVectorKey *key2 = new 
vtkInformationDoubleVectorKey(name,
                                                                          location,
                                                                          length);
   double values[] = {0.1, 0.2, 0.3};
   info->Set(key, values[0], values[1],values[2]);

   // extract the key
   // double *vals = info->Get(key2); // this throws a seg fault....

   double *vals = info->Get(key);
   std::cout << "extracted values are: " << vals[0] << ", " << vals[1] 
<< ", " << vals[2] << '\n';
   array->Delete();
   // array->Delete will also delete key (do not call delete key)
}




More information about the vtkusers mailing list