[Paraview] vtkDoubleArray with >3 components

Bruce Jones bruce.david.jones at gmail.com
Wed Jul 22 14:28:43 EDT 2015


Hey Guys,

I have created a minimum working example (attached). This example is built
as a paraview plugin, build environment specified below. The plugin takes
point data as input (sample attached), and just does the memory allocation
and creation of output arrays as I have previously described. On my
machine, as soon as I apply the filter, paraview crashes. Commenting line
107 in vtkHeapCorruption.cxx allows the filter to run, but the output is
then limited to single component arrays, where some of the input arrays
have 3 or 9 components.

QT 4.8.6, Visual Studio 2010, Built against Paraview commit SHA1
ID: 70155c17cfe7c2f547bb25ddb859a65518896ee3

On Wed, 22 Jul 2015 at 11:21 Bruce Jones <bruce.david.jones at gmail.com>
wrote:

> Thanks Berk,
>
> I have tried with both zero and 1 as the save argument and am getting the
> same behavior. As before, if I comment the SetNumberOfComponents call (or
> set number of components to 1) everything works, except I only have single
> component arrays in the output.
>
> I'm going to put together a minimum working example with the issue this
> afternoon so that I can show you guys in more detail.
>
> Cheers,
> Bruce
>
> On Wed, 22 Jul 2015 at 11:09 Berk Geveci <berk.geveci at kitware.com> wrote:
>
>> The solution is in the documentation:
>>
>>   // This method lets the user specify data to be held by the array.  The
>>   // array argument is a pointer to the data.  size is the size of the
>>   // array supplied by the user.  Set save to 1 to keep the class from
>>   // deleting the array when it cleans up or reallocates memory.  The
>> class
>>   // uses the actual array provided; it does not copy the data from the
>>   // suppled array. If specified, the delete method determines how the
>> data
>>   // array will be deallocated. If the delete method is
>>   // VTK_DATA_ARRAY_FREE, free() will be used. If the delete method is
>>   // DELETE, delete[] will be used. The default is FREE.
>>
>> You are passing 0 for the "save" argument, which means that VTK will
>> delete the array when the VTK array object is deleted, which would cause a
>> double delete/free. Use 1 as the second argument of SetArray() if you are
>> managing the memory. Also, make sure that the VTK array is not used after
>> you free your array.
>>
>> Best,
>> -berk
>>
>> On Wed, Jul 22, 2015 at 9:56 AM, Bruce Jones <bruce.david.jones at gmail.com
>> > wrote:
>>
>>> Hi Guys,
>>>
>>> I have just tried it as you suggested, doing SetNumberOfComponents then
>>> SetArray, without doing SetNumberOfTuples. Unfortunately I am still getting
>>> the same behaviour.
>>>
>>> Cheers,
>>> Bruce
>>>
>>> On Wed, 22 Jul 2015 at 09:44 Berk Geveci <berk.geveci at kitware.com>
>>> wrote:
>>>
>>>> Actually, don't call SetNumberOfTuples() at all if you are using
>>>> SetArray(). It will unnecessarily allocate memory if you call it before
>>>> SetArray().
>>>>
>>>> -berk
>>>>
>>>> On Wed, Jul 22, 2015 at 9:40 AM, Shawn Waldon <shawn.waldon at kitware.com
>>>> > wrote:
>>>>
>>>>> Hi Bruce,
>>>>>
>>>>> SetNumberOfTuples reallocates the internal datastructure of the data
>>>>> array to be the new required size based on the new number of components and
>>>>> number of tuples.  This (combined with trying to clean up the old data from
>>>>> SetArray) may be causing the heap corruption you are seeing.  Try setting
>>>>> the number of components and number of tuples before adding the data to the
>>>>> array.
>>>>>
>>>>> HTH,
>>>>> Shawn
>>>>>
>>>>> On Tue, Jul 21, 2015 at 1:37 PM, Bruce Jones <
>>>>> bruce.david.jones at gmail.com> wrote:
>>>>>
>>>>>> Hi Cory,
>>>>>>
>>>>>> Sorry for the delayed response, I've hit another more serious bug
>>>>>> which has been diverting my attention from this.
>>>>>>
>>>>>> Thanks for the state file, I haven't got it to work as I don't have
>>>>>> numpy set up, but I have also got this working with 9 component arrays in
>>>>>> paraview using a python script. However, I am now trying to port my python
>>>>>> plugin to c++ which is where I am seeing the error.
>>>>>>
>>>>>> In my plugin I want to output a vtkarray for each vtkarray in the
>>>>>> input dataset. The values in the output vtkarrays are interpolated from
>>>>>> those in the input vtkarrays. Rather than working directly on vtkarray
>>>>>> objects, I am allocating c++ arrays for each vtkarray, performing the
>>>>>> interpolation, then creating vtkDoubleArrays and using SetArray to pass a
>>>>>> pointer to my allocated c++ arrays to the output vtkarrays. The code for
>>>>>> setting up the output vtkarrays is roughly as follows
>>>>>>
>>>>>>                 vtkPointData *pointData = input->GetPointData();
>>>>>>                 vtkDataArray *dArray = pointData->GetArray(i);
>>>>>> vtkDoubleArray *vtkDArrayAve = vtkDoubleArray::New();
>>>>>> vtkDArrayAve->SetName(dArray->GetName());
>>>>>> vtkDArrayAve->SetArray(dArraysAve[i],hgrid->GetTotalCells(),0);
>>>>>> vtkDArrayAve->SetNumberOfComponents(numComponents[i]);
>>>>>> vtkDArrayAve->SetNumberOfTuples(hgrid->GetTotalCells());
>>>>>> output->GetPointData()->AddArray(vtkDArrayAve);
>>>>>>
>>>>>> dArraysAve is an array of pointers pointing to my c++ arrays
>>>>>> hgrid->GetTotalCells() returns the number of data points for my output
>>>>>>
>>>>>> I also have another mode of operation for this plugin, where I
>>>>>> perform the interpolation differently, in that function, if I make the call
>>>>>> to SetNumberOfComponents() some heap corruption occurs and I get a segfault
>>>>>> when I try to subsequently free my c++ arrays. Commenting out the call to
>>>>>> SetNumberOfComponents avoids the segfault, but then I am limited to single
>>>>>> component arrays. This is the more serious bug I mentioned, initially I
>>>>>> figured they were unrelated, but I can't deny the number of components
>>>>>> coincidence.
>>>>>>
>>>>>> Cheers,
>>>>>> Bruce
>>>>>>
>>>>>> On Thu, 16 Jul 2015 at 14:41 Cory Quammen <cory.quammen at kitware.com>
>>>>>> wrote:
>>>>>>
>>>>>>> Hi Bruce,
>>>>>>>
>>>>>>> I have attached a ParaView state file with a Programmable Source
>>>>>>> that produces a vtkPolyData with 100 random points and a point data array
>>>>>>> with 9 components. It seems to display the various component ranges just
>>>>>>> fine.
>>>>>>>
>>>>>>> Best,
>>>>>>> Cory
>>>>>>>
>>>>>>> On Thu, Jul 16, 2015 at 11:14 AM, Cory Quammen <
>>>>>>> cory.quammen at kitware.com> wrote:
>>>>>>>
>>>>>>>> Hi Bruce,
>>>>>>>>
>>>>>>>> I haven't seen this, but ParaView may be interpreting this array as
>>>>>>>> a tensor field. Do you have a smallish test data file that you can share
>>>>>>>> that shows the problem?
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>> Cory
>>>>>>>>
>>>>>>>> On Sat, Jul 11, 2015 at 11:14 AM, Bruce Jones <
>>>>>>>> bruce.david.jones at gmail.com> wrote:
>>>>>>>>
>>>>>>>>> I am creating vtkDoubleArrays based on averaging some input data
>>>>>>>>> to a reduced set of points. The input data includes various vtk arrays
>>>>>>>>> which have 1, 3 and 9 components.
>>>>>>>>>
>>>>>>>>> After averaging, I am writing the data to c++ arrays, and creating
>>>>>>>>> a new vtkDoubleArray using the SetArray() function to pass the c++ arrays.
>>>>>>>>> This works fine for 1 and 3 component arrays, however for the 9 component
>>>>>>>>> arrays paraview shows that every element is set to 0.
>>>>>>>>>
>>>>>>>>> If I hardcode it so that 9 component arrays become 3 component
>>>>>>>>> arrays (reading only the first 3 components from the input array), then I
>>>>>>>>> get the correct data for the first 3 components, though I obviously need
>>>>>>>>> the other 6 components in the end.
>>>>>>>>>
>>>>>>>>> Has anyone encountered this before?
>>>>>>>>>
>>>>>>>>> Cheers,
>>>>>>>>> Bruce
>>>>>>>>>
>>>>>>>>> _______________________________________________
>>>>>>>>> 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 ParaView Wiki at:
>>>>>>>>> http://paraview.org/Wiki/ParaView
>>>>>>>>>
>>>>>>>>> Search the list archives at:
>>>>>>>>> http://markmail.org/search/?q=ParaView
>>>>>>>>>
>>>>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>>>>> http://public.kitware.com/mailman/listinfo/paraview
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Cory Quammen
>>>>>>>> R&D Engineer
>>>>>>>> Kitware, Inc.
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> Cory Quammen
>>>>>>> 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 ParaView Wiki at:
>>>>>> http://paraview.org/Wiki/ParaView
>>>>>>
>>>>>> Search the list archives at: http://markmail.org/search/?q=ParaView
>>>>>>
>>>>>> Follow this link to subscribe/unsubscribe:
>>>>>> http://public.kitware.com/mailman/listinfo/paraview
>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> 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 ParaView Wiki at:
>>>>> http://paraview.org/Wiki/ParaView
>>>>>
>>>>> Search the list archives at: http://markmail.org/search/?q=ParaView
>>>>>
>>>>> Follow this link to subscribe/unsubscribe:
>>>>> http://public.kitware.com/mailman/listinfo/paraview
>>>>>
>>>>>
>>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/paraview/attachments/20150722/c4973fd2/attachment-0001.html>
-------------- next part --------------
// gridFromSPH code
#include "vtkHeapCorruption.h"

// VTK code
#include "vtkObjectFactory.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkImageData.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkDoubleArray.h"


vtkStandardNewMacro(vtkHeapCorruption);

//----------------------------------------------------------------------------
vtkHeapCorruption::vtkHeapCorruption()
{
	memPrevAllocated = false;
}

//----------------------------------------------------------------------------
vtkHeapCorruption::~vtkHeapCorruption()
{
	FreeMemory();
}

//----------------------------------------------------------------------------
void vtkHeapCorruption::PrintSelf(ostream& os, vtkIndent indent)
{
	this->Superclass::PrintSelf(os,indent);
}

int vtkHeapCorruption::FillInputPortInformation(
	int vtkNotUsed(port), vtkInformation* info)
{
	info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");
	return 1;
}

int vtkHeapCorruption::RequestInformation (
	vtkInformation * vtkNotUsed(request),
	vtkInformationVector ** vtkNotUsed( inputVector ),
	vtkInformationVector *outputVector)
{
	vtkInformation* outInfo = outputVector->GetInformationObject(0);

	outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
		0, 50,
		0, 50,
		0, 5);

  	double origin[3] = {0.0,0.0,0.0};
	outInfo->Set(vtkDataObject::ORIGIN(),origin,3);

	double spacing[3] = {0.1,0.1,0.1};
	outInfo->Set(vtkDataObject::SPACING(),spacing,3);

	return 1;
}

int vtkHeapCorruption::RequestData(
	vtkInformation*,
	vtkInformationVector** inputVector,
	vtkInformationVector* outputVector)
{
	vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
	vtkDataSet *input = vtkPolyData::SafeDownCast(
		inInfo->Get(vtkDataObject::DATA_OBJECT()));

	vtkInformation *outInfo = outputVector->GetInformationObject(0);
	vtkImageData *output = vtkImageData::SafeDownCast(
		outInfo->Get(vtkDataObject::DATA_OBJECT()));

	output->SetExtent(
		outInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()));

	vtkPointData *pointData = input->GetPointData();

	numArrays = pointData->GetNumberOfArrays();

	// Preform memory allocation
	// Note: Cannot free memory at end of this all to RequestData otherwise
	// we cannot see the result in paraview, so we free this memory on the
	// next timestep and reallocate - could/should be done differently
	if(memPrevAllocated) FreeMemory();
	dArraysAve = (double**)malloc(numArrays*sizeof(double*));
	numComponents = (int*)malloc(numArrays*sizeof(int));
	for(int dataArray = 0; dataArray<numArrays; dataArray++)
	{
		vtkDataArray *dArray = pointData->GetArray(dataArray);
		numComponents[dataArray] = dArray->GetNumberOfComponents();
		dArraysAve[dataArray] = (double*)malloc(numComponents[dataArray]*50*50*5*sizeof(double));
	}
	memPrevAllocated = true;

	// Create an output array for each input array
	for(int dataArray=0; dataArray<numArrays; dataArray++)
	{
		vtkDataArray *dArray = pointData->GetArray(dataArray);

		vtkDoubleArray *vtkDArrayAve = vtkDoubleArray::New();
		vtkDArrayAve->SetName(dArray->GetName());
		// Comment the next line and the code will "work" however we can only have
		// single component arrays in that case
		vtkDArrayAve->SetNumberOfComponents(numComponents[dataArray]);
		vtkDArrayAve->SetArray(dArraysAve[dataArray],50*50*5,1);
		output->GetPointData()->AddArray(vtkDArrayAve);
	}

	return 1;
}

void vtkHeapCorruption::FreeMemory()
{
	for(int i = 0; i<numArrays; i++)
	{
		free(dArraysAve[i]);
	}
	free(dArraysAve);
	free(numComponents);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: HeapCorruption.xml
Type: text/xml
Size: 1103 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/paraview/attachments/20150722/c4973fd2/attachment-0001.xml>
-------------- next part --------------
cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)

if (NOT ParaView_BINARY_DIR)
  find_package(ParaView REQUIRED)
  include(${PARAVIEW_USE_FILE})
endif()

# Set a consistent MACOSX_RPATH default across all CMake versions.
# When CMake 2.8.12 is required, change this default to 1.
# When CMake 3.0.0 is required, remove this block (see CMP0042).
if(NOT DEFINED CMAKE_MACOSX_RPATH)
  set(CMAKE_MACOSX_RPATH 0)
endif()

include(ParaViewPlugins)

SET(PLUGIN_NAME HeapCorruption)
SET(PLUGIN_VERSION "1.0")

# create a paraview plugin containing server manager xml and the server
# manager classes to build
# this plugin can be loaded on the server side

ADD_PARAVIEW_PLUGIN(HeapCorruption "1.0"
  SERVER_MANAGER_XML HeapCorruption.xml
  SERVER_MANAGER_SOURCES vtkHeapCorruption.cxx)
-------------- next part --------------
#ifndef __GridFromSPH_h
#define __GridFromSPH_h

#include "vtkImageAlgorithm.h"

class vtkHeapCorruption : public vtkImageAlgorithm
{
public:
	static vtkHeapCorruption* New();
	vtkTypeMacro(vtkHeapCorruption, vtkImageAlgorithm);

	// VTK/Paraview event handlers
	virtual int FillInputPortInformation(int port, vtkInformation* info);

	virtual int RequestInformation (vtkInformation *,
		vtkInformationVector **,
		vtkInformationVector *);

	virtual int RequestData(vtkInformation* request,
		vtkInformationVector** inputVector,
		vtkInformationVector* outputVector);

	void PrintSelf(ostream& os, vtkIndent indent);

protected:
	vtkHeapCorruption();
	~vtkHeapCorruption();

private:
  vtkHeapCorruption(const vtkHeapCorruption&);  // Not implemented.
  void operator=(const vtkHeapCorruption&);  // Not implemented.
  int numArrays;
  double **dArraysAve;
  int *numComponents;
  bool memPrevAllocated;

  // Utility functions
  void FreeMemory();
};

#endif
-------------- next part --------------
A non-text attachment was scrubbed...
Name: solid_ts_00000100.vtp
Type: application/octet-stream
Size: 139238 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/paraview/attachments/20150722/c4973fd2/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: solid_ts_00000000.vtp
Type: application/octet-stream
Size: 93072 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/paraview/attachments/20150722/c4973fd2/attachment-0003.obj>


More information about the ParaView mailing list