itk filereader 导致访问冲突异常

发布于 2024-09-30 12:14:09 字数 1300 浏览 2 评论 0原文

我遇到访问冲突异常的问题。 我正在使用 itk 并使用它的文件阅读器读取文件。

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;
}

这是一个函数,该类还包含 2 个全局变量:

ImageFileReaderType::Pointer fileReader;
ThreeDImageFloatType *t3dim;

现在,如果您从我的 main 方法调用类中的函数并尝试访问返回值,例如 t3dim->GetLargestPossibleRegion().GetSize ();。我收到访问冲突错误。重要的是要注意,如果我不外包代码并将其放在主方法中,它就会像魅力一样工作。可能是什么问题?我该如何解决这个问题?

[编辑] 我尝试用 const char* 文件名替换字符串文件名。主要方法如下所示。

MyClass imIO;

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

同样,如果我将函数中的代码完全放入 main 方法中,它就会起作用。

[/编辑]

[题外话] 也许版主可以将其标记为 itk,因为这是一个 itk 特定问题? [/题外话]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-10-07 12:14:09

我刚刚自己找到了答案。问题的解决方案就在这一行:

fileReader = ImageFileReaderType::New();

它是一个智能指针。因此,当函数返回时,它就会被取消注册。因此,从该函数接收到的指向内部缓冲区(读取文件)的指针不能再使用。虽然指针指向实际内存,但无法再访问它。访问冲突错误。

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.

坠似风落 2024-10-07 12:14:09

我在这里看到的唯一问题是您通过复制将 filename 传递给函数。一旦函数返回,从调用 c_str() 接收到的指针就无效。

ImageFileReaderType 是否保留对此指针的引用并在对 GetSize() 的调用中使用它?

如果是这样,那么您可能需要尝试其他一些策略来使 filename 变量在 t3dim 的整个生命周期中保持活动状态。

更新的后续:这可能听起来像是另一个愚蠢的尝试,但是您会检查空指针吗?您确定所有GetOutput()方法都返回有效对象吗?许多 C++ 库(不幸的是)更喜欢返回空指针而不是抛出异常...

既然你说过,如果你把所有东西都放在 main() 中,它就可以工作,我认为你的代码中存在一些微妙之处。转换以获得当前代码。我们可以看看两个样本进行比较吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文