Access Reading Violation when writing a DICOM with vtkDICOMWriter

-1

I'm trying to write vtkImageData as a DICOM. I keep getting an "Access Reading Violation" when I try to write the image.

Unhandled exception at 0x00007FFDA30ECA50 : 0xC0000005: Access violation reading location 0x000001BD38D5C000

Here is my code:

vtkSmartPointer<vtkDICOMWriter> dcmWriter = vtkSmartPointer<vtkDICOMWriter>::New();
dcmWriter->SetInputData(testDat);
dcmWriter->SetFileName(fullPath.toStdString().c_str());
dcmWriter->Update(); // this line breaks
dcmWriter->Write(); 

testDat is a vtkSmartPointer<vtkImageData> type and has data in it. Any thoughts on whats causing the error? I can't find anything similar online.

I followed this example: https://github.com/dgobbi/vtk-dicom/blob/master/Examples/TestDICOMWriter.cxx

I don't have metadata, but that shouldn't be a problem.

c++
qt
vtk
dicom
asked on Stack Overflow May 29, 2019 by Lambda1010 • edited May 29, 2019 by drescherjm

2 Answers

0

I need to make some guesses here because you didn't post all of your code, but I suspect that the problem happens in the following line:

dcmWriter->SetFileName(fullPath.toStdString().c_str());

toStdString() is most probably returning a temporary std::string (fullPath looks like a Qt QString), on which you call c_str(). After the statement, your temporary is destroyed and whatever you have passed to SetFileName is now a dangling pointer. Hence the segfault.

Try the following instead::

const auto pathString = fullPath.toStdString();
dcmWriter->SetFileName(pathString.c_str());

This should hopefully work fine. Even if not, it definitely is an issue with your code.

answered on Stack Overflow May 29, 2019 by andreee • edited May 29, 2019 by andreee
0

These lines are from the example you posted:

writer->SetFilePrefix("/tmp");
writer->SetFilePattern("%s/IM-0001-%04.4d.dcm");

and you use

dcmWriter->SetFileName(fullPath.toStdString().c_str());

It seems that vtkDICOMWriter writes several files so you probably need to provide a file pattern. Anyway, it's hard to guess why it gives a reading error and it's hard to help if you don't post a full working example.

Last, vtkDICOMWriter is not a class from vtk, it was released separetely (it seems) in 2017. This means it's not tested against the rest of VTK at every new release.

answered on Stack Overflow May 31, 2019 by L.C.

User contributions licensed under CC BY-SA 3.0