0
votes

I have a problem with an Access Violation Exception. I am using itk and read a File with it's file reader.

ThreeDImageFloatType* MyClass::loadImage(std::string filename){
const char* cfilename = filename.c_str();
fileReader = ImageFileReaderType::New();
fileReader->SetFileName(cfilename);

try{ 
    fileReader->Update();
}catch( ... ) {
    std::cerr << "failed to read file " << filename << std::endl; 
}

CastFilterType::Pointer castFilter = CastFilterType::New();
castFilter->SetInput(fileReader->GetOutput());


castFilter->Update();

//ThreeDImageFloatType *t3dim = castFilter->GetOutput();
t3dim = castFilter->GetOutput();
return t3dim;
}

This is a function the class also contains 2 global variables:

ImageFileReaderType::Pointer fileReader;
ThreeDImageFloatType *t3dim;

Now if you call the the function in the class from for example my main method and try to access the return value, something like t3dim->GetLargestPossibleRegion().GetSize();. I get an access violation error. It is important to notice if i don't outsource the code, and have it within the main method, it works like a charm. What could be the problem? How do i fix that?

[edit] I tried replacing the string filename with a const char* filename. The main method looks like this.

MyClass imIO;

const char* filename = "path to file";
ThreeDImageFloatType *t3dim = imIO.loadImage(filename);
t3dim->GetLargestPossibleRegion().GetSize();

Again if i put the code from the function completly in the main method it works.

[/edit]

[offtopic] maybe a moderator can tag it as itk, since it is an itk specific question? [/offtopic]

2
ITK - itk.org - ITK is an open-source, cross-platform system that provides developers with an extensive suite of software tools for image analysis.Mike Clark
I know that, i am using it. ???.scigor
Of course you know what ITK is, but other people will not know what it is. I did not know. I put the link there so that other people could more easily find out what it is, and read its documentation if they liked.Mike Clark
Didn't think about that, your right.scigor

2 Answers

1
votes

I just found the answer myself. The Solution to the Problem lies within this line:

fileReader = ImageFileReaderType::New();

It's a smart pointer. So when the function returns, it gets unregistered. So the pointer received from that function to an internal buffer (the read file), can't be used any more. While the pointer points to actual memory, it can't be accessed any more. Access Violation Error.

0
votes

The only issue I see here is that you are passing filename by copy to the function. The pointer received from the call to c_str() is not valid once the function returns.

Is ImageFileReaderType keeping a reference to this pointer and using it in the call to GetSize()?

If so, then you might want to try out some other stategy to keep the filename variable alive throughout t3dim's lifetime.

Follow-up to your update: this may sound like another silly attempt, but do you check for null pointers? Are you sure that all the GetOutput()methods return valid objects? A lot of C++ libraries (unfortunately) prefer returning null pointers to throwing exceptions...

Since you said that if you put everything in main() it works, I assume there is some subtlety going on in your transformation to obtain the current code. Can we see both samples to compare?