[vtkusers] vtkInformation access with equivalent keys

David E DeMarle dave.demarle at kitware.com
Thu Mar 8 09:38:19 EST 2012


What is happening is that key1 and key2 are not actually the same thing in
the same way that &a and &b are not the same thing below.
int a = 1;
int b = 1;

Thus info->Has(key1) returns true, info->Has(key2) returns false,
info->Get(key1) returns the value you stored, and info->Get(key2) walks off
a null pointer and crashes merrily.

The usage pattern I've seen in VTK is to have a static member variable of
each particular key that everything sees and uses to get that particular
piece of information. See vtkStreamingDemandDrivenPipeline.{h,cxx}
REQUEST_UPDATE_EXTENT() and the macros at the bottom of vtkInformationKey.h
for example to see it in practice.

By staring really hard at the oxygen, I've almost convinced myself that you
could also do what you are trying to do by:
calling vtkInformation CopyEntries() to produce a vtkInformationKeyVectorKey
calling vtkInformationKeyVectorKey::Get(,idx) to return each
vtkInformaitonKey
calling vtkInformation::GetClassName() and GetLocation() to get strings to
compare against.
but I haven't tried it myself. :)

Let us know what you find and please consider adding an example to the vtk
examples wiki that demonstrates.

David E DeMarle
Kitware, Inc.
R&D Engineer
21 Corporate Drive
Clifton Park, NY 12065-8662
Phone: 518-881-4909


On Wed, Mar 7, 2012 at 6:00 PM, Alexander Pletzer <pletzer at txcorp.com>wrote:

> 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)
> }
>
> ______________________________**_________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/**
> opensource/opensource.html<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 <http://www.vtk.org/Wiki/VTK_FAQ>
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/**listinfo/vtkusers<http://www.vtk.org/mailman/listinfo/vtkusers>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120308/db58789f/attachment.htm>


More information about the vtkusers mailing list