[vtkusers] How to avoid this kind of memory leak in surface reconstruction?
赵暖
zhao_nuan at 126.com
Mon Apr 25 22:29:41 EDT 2005
Hi,
I'm puzzled by this question for some time, and I want to get help from you very much. I write codes like that in a following part, but when I finished this, I found there are still about 4m memory is not be released.Can you tell me what can I do after use this functionsOr are there any misuse in my codes? I have to use this function for many times, so if
the memory leak still happens, the program will dump at last.Thank you for your attetion and I'm look forward to your reply!
void heart()
{
int imageWidth = right - left + 1;
int imageHeight = down - up + 1;
int imageNum = top - bottom + 1;
int mydim[3] = {imageHeight, imageWidth, imageNum};
size = mydim[0] * mydim[1] * mydim[2];
unsigned char* dat = (unsigned char*)malloc(size);
for (int k = bottom; k <= top; k++)
{
for (int j = left; j <= right; j++)
{
for (int i = up; i <= down; i++)
{
dat[(k-bottom) * mydim[0] * mydim[1] + (j-left) * mydim[0] + (i-up)] = temp[k * dim[0] * dim[1] + j * dim[0] + i];
}
}
}
free(temp);
// vtkImageData *id = vtkImageData::New();
// id->SetDimensions{239,239,120};
vtkUnsignedCharArray *array = vtkUnsignedCharArray::New();
array ->SetVoidArray( dat, mydim[0] * mydim[1] * mydim[2], 1);
// free(temp);
vtkImageData* imageData = vtkImageData::New() ;
imageData ->GetPointData() ->SetScalars(array) ;
imageData ->SetDimensions(mydim) ;
imageData ->SetScalarType(VTK_UNSIGNED_CHAR) ;
imageData ->SetSpacing(1.0, 1.0, 1.0) ;
imageData ->SetOrigin(0.0, 0.0, 0.0);
vtkMarchingCubes *skinExtractor = vtkMarchingCubes::New();
skinExtractor->SetInput(imageData);
skinExtractor->SetValue(50, 100);
vtkStripper *skinStripper = vtkStripper::New();
skinStripper->SetInput(skinExtractor->GetOutput());
vtkPolyDataMapper *skinMapper = vtkPolyDataMapper::New();
skinMapper->SetInput(skinStripper->GetOutput());
vtkActor *skin = vtkActor::New();
skin->SetMapper(skinMapper);
skin->GetProperty()->SetAmbientColor(0.75f, 0.75f, 1.0f);
skin->GetProperty()->SetDiffuseColor( 1, .25, .25);
skin->GetProperty()->SetSpecular(0.6);
skin->GetProperty()->SetColor(1, .25, .25);
skin->GetProperty()->SetSpecularPower(10.0f);
vtkRenderer *ren = vtkRenderer::New();
vtkRenderWindow *renWindow = vtkRenderWindow::New();
renWindow->AddRenderer(ren);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow (renWindow);
ren->AddActor(skin);
renWindow->SetDesiredUpdateRate(5.0);
ren->SetBackground( 1, 1, 1);
renWindow->SetSize(400, 400);
ren->GetActiveCamera()->Zoom(1.0);
renWindow->Render();
iren->Start();
renWindow->RemoveRenderer(ren);
ren->RemoveActor(skin);
free(dat);
array->Delete();
imageData->Delete();
skinExtractor->Delete();
skinStripper->Delete();
skinMapper->Delete();
renWindow->Delete();
ren->Delete();
skin->Delete();
iren->Delete();
}
zhao_nuan at 126.com
2005-04-26
More information about the vtkusers
mailing list