tags:

views:

28

answers:

2

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]

A: 

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?

André Caron
No that's not it. The filename is just the name of the file, which should be loaded. But i still tested it to be absolutely sure and added a const char* parameter to the function and used it instead. So the filename is accessable through the whole application livecycle. I still get the same error.
inf.ig.sh
@inf.ig.sh: can you post some client code? I don't think there is enough in the posted listing to diagnose your error.
André Caron
I am pretty sure thats not the issue in the main method it looks right now like this: MyClass imIO;const char* filename = "path to file";ThreeDImageFloatType *t3dim = imIO.loadImage(filename)after that i try to access the t3dim pointer as described above. Again if i put the code completly in the main method it works.
inf.ig.sh
I see that code isn't really readable in comments so i will update the question.
inf.ig.sh
Right. c_str() does not return a copy of the characters in the std::string. If the std::string goes out of scope, then the c_str() you got out of it may become invalid. This may not be the problem, but it's something to pay attention to.
Mike Clark
@inf.ig.sh: edited answer to reflect your edit.
André Caron
I of course debugged it before asking the question. No it's not a null pointer.
inf.ig.sh
sry found the answaer by myself
inf.ig.sh
A: 

Unfortunatly i have to answear the question myself. I just found the answear. 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 actuall memory, it can't be accessed any more. Access Violation Error.

inf.ig.sh
You should mark the answer as accepted so you don't get a low access rate :-)
André Caron