-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.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Lambda1010
  • 369
  • 2
  • 3
  • 13

2 Answers2

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.

andreee
  • 4,459
  • 22
  • 42
  • Unfortunately, this did not work. I even tried hard coding the path using " " and I still get the access violation reading location. Its crashing on the `dcmWriter->Update()`, and if I comment out the `Update()` line, it will crash on the `dcmWriter->Write()` line. – Lambda1010 May 29 '19 at 16:51
  • Are you using the correct `vtk-dicom` binaries for your compiler? – drescherjm May 29 '19 at 16:56
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.

L.C.
  • 1,098
  • 10
  • 21