Catch error on vtkDICOMWriter

I have the following code:

vtkDICOMMRGenerator* pGenerator = vtkDICOMMRGenerator::New();
vtkDICOMWriter* pWriter = vtkDICOMWriter::New();
pWriter->SetInputConnection(m_pImageExtractor->GetOutputPort());
pWriter->SetGenerator(pGenerator);
pWriter->SetPatientMatrix(m_pDICOMReader->GetPatientMatrix());
pWriter->SetMetaData(m_pDICOMReader->GetMetaData());
pWriter->SetFilePrefix(sPathName);
pWriter->SetFilePattern(_T("%s/IM-0001-%04.4d.dcm"));
try
{
	pWriter->Write();
}
catch (std::exception& error)
{
	m_sState = error.what();
}

sPathName has value: d:\tempx\output\

but at Write method, I got crash:

How can correctly catch the error in VTK ? Is this way, I would know the source of my error …

P.S. The example of vtkDICOMWriter I’ve taken from here: https://github.com/dgobbi/vtk-dicom/blob/master/Examples/TestDICOMWriter.cxx

You only try to catch C++ exceptions and you cannot catch all exceptions anyway, so I would recommend to focus on fixing the root cause of the error.

Using _T for string literals is decade-old technology, it never got widely adopted and I don’t think anybody tested it with VTK in recent years. Instead, I would recommend to use UTF-8 everywhere. In current Windows versions, you can even set the application’s code page to UTF-8.

Hello,

The sequence \t is an escape sequence for the tab character in C-like languages. Try using \\t to have a \t in your path string instead of a tab.

cheers,

Paulo

You can also use / in place of \ as the recent versions of Windows regonize paths with forward slashes, then you can avoid that kind of bug.

1 Like

I guess is not this problem, the code looks like that:

_T(“D:\\Tempx\\Output”)

Make sure you have full access rights to the path.

I have the following code:

vtkDICOMMRGenerator* pGenerator = vtkDICOMMRGenerator::New();
vtkDICOMWriter* pWriter = vtkDICOMWriter::New();
pWriter->SetInputConnection(m_pImageExtractor->GetOutputPort());
pWriter->SetGenerator(pGenerator);
pWriter->SetPatientMatrix(m_pDICOMReader->GetPatientMatrix());
pWriter->SetMetaData(m_pDICOMReader->GetMetaData());
pWriter->SetFilePrefix(_T("E:/Output/"));
pWriter->SetFilePattern(_T("%s/IM-0001-%04.4d.dcm"));
TRACE(">>>>>>%s|%s|%s\n", pWriter->GetFileName(), pWriter->GetFilePattern(), pWriter->GetFilePrefix());
pWriter->Write();
pWriter->Delete();
pGenerator->Delete();

With the following outcome:

>>>>>>(null)|%s/IM-0001-%04.4d.dcm|E:/Output/

But the strange thing is coming now: in Debug is crashing as I wrote in first post, in Release is run without problem … why ?

If it is crashing in debug mode then it’s awesome, you can then see exactly what goes wrong. Just run the code in a debugger and have a look at the call stack, inspect variables, etc.

I’ll tried that, but only on my app variables, because I don’t have cxx source files from VTK or VTK-DICOM … I got header files and lib files for VTK and VTK-DICOM and I only included in my project.

Anyway, at the line

pWriter->Write();

my program stop with that crash (see first post).

@lassoan I guess I cannot do that, I could find what is the problem inside of VTK-DICOM class vtkDICOMWriter but I don’t have cxx file, I got from someone only header files and lib files already compiled.

Do you use Visual Studio? If so, you need to double check whether your peer used the same compiler to build the DLLs. If your peer used Visual Studio 2015 to compile VTK, then you should compile your program with it as well.

I think I found the source of the issue: for patient matrix I used vtkDICOMReader but for input connection I used vtkImageExtractComponents.