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.<div>int a = 1;</div><div>int b = 1;<br><div><br></div><div>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.<div>
<br></div><div>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.</div>
<div><br></div><div>By staring really hard at the oxygen, I've almost convinced myself that you could also do what you are trying to do by:</div><div>calling vtkInformation CopyEntries() to produce a vtkInformationKeyVectorKey</div>
<div>calling vtkInformationKeyVectorKey::Get(,idx) to return each vtkInformaitonKey</div><div>calling vtkInformation::GetClassName() and GetLocation() to get strings to compare against.</div><div>but I haven't tried it myself. :)</div>
<div><div><br></div><div>Let us know what you find and please consider adding an example to the vtk examples wiki that demonstrates.</div><div><br clear="all">David E DeMarle<br>Kitware, Inc.<br>R&D Engineer<br>21 Corporate Drive<br>
Clifton Park, NY 12065-8662<br>Phone: 518-881-4909<br>
<br><br><div class="gmail_quote">On Wed, Mar 7, 2012 at 6:00 PM, Alexander Pletzer <span dir="ltr"><<a href="mailto:pletzer@txcorp.com">pletzer@txcorp.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
The following small code shows how to store and access information by key. The code runs fine but if I comment out line<br>
<br>
// double *vals = info->Get(key2); // this throws a seg fault....<br>
<br>
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.<br>
<br>
Thanks in advance for any help.<br>
<br>
--Alex<br>
<br>
<br>
// vtk includes<br>
#include <vtkDoubleArray.h><br>
#include <vtkInformation.h><br>
#include <<u></u>vtkInformationDoubleVectorKey.<u></u>h><br>
<br>
// std includes<br>
#include <iostream><br>
<br>
int main() {<br>
<br>
// create a 2-element array<br>
vtkDoubleArray* array = vtkDoubleArray::New();<br>
array->SetName("array");<br>
array->SetNumberOfComponents(<u></u>1);<br>
array->SetNumberOfTuples(2);<br>
array->SetValue(0, 1.);<br>
array->SetValue(1, 2.);<br>
<br>
// access the info (presently none stored)<br>
vtkInformation* info = array->GetInformation();<br>
<br>
// add one attribute, a double vector<br>
const char *name = "myKey";<br>
const char *location = "MyClass"; //<br>
const int length = 3;<br>
vtkInformationDoubleVectorKey *key = new vtkInformationDoubleVectorKey(<u></u>name,<br>
location,<br>
length);<br>
vtkInformationDoubleVectorKey *key2 = new vtkInformationDoubleVectorKey(<u></u>name,<br>
location,<br>
length);<br>
double values[] = {0.1, 0.2, 0.3};<br>
info->Set(key, values[0], values[1],values[2]);<br>
<br>
// extract the key<br>
// double *vals = info->Get(key2); // this throws a seg fault....<br>
<br>
double *vals = info->Get(key);<br>
std::cout << "extracted values are: " << vals[0] << ", " << vals[1] << ", " << vals[2] << '\n';<br>
array->Delete();<br>
// array->Delete will also delete key (do not call delete key)<br>
}<br>
<br>
______________________________<u></u>_________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/<u></u>opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the VTK FAQ at: <a href="http://www.vtk.org/Wiki/VTK_FAQ" target="_blank">http://www.vtk.org/Wiki/VTK_<u></u>FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.vtk.org/mailman/listinfo/vtkusers" target="_blank">http://www.vtk.org/mailman/<u></u>listinfo/vtkusers</a><br>
</blockquote></div><br></div></div></div></div>