违规访问时编译(0xC0000005)

Violation access in time compilation (0xC0000005)

本文关键字:0xC0000005 编译 访问      更新时间:2023-10-16

我要做的过程是使FFT到图像(存储在" imagen "中),然后,将其与过滤器' H '相乘,之后,逆FFT也将完成。代码如下所示:

int ancho;
int alto;
ancho=ui.imageframe->imagereader->GetBufferedRegion().GetSize()[0];     //ancho=widht of the image
alto=ui.imageframe->imagereader->GetBufferedRegion().GetSize()[1];      //alto=height of the image
double *H ;
H =matrix2D_H(ancho,alto,eta,sigma); // H is calculated
// We want to get: F= fft(f) ; H*F ; f'=ifft(H*F)
// Inicialization of the neccesary elements for the calculation of the fft
fftw_complex *out;
fftw_plan p;
int N= (ancho/2+1)*alto; //number of points of the image
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N);
double *in = (double*) imagen.GetPointer(); // conversion of itk.smartpointer --> double*
p = fftw_plan_dft_r2c_2d(ancho, alto, in, out, FFTW_ESTIMATE); // FFT planning
fftw_execute(p); // FFT calculation
/* Multiplication of the Output of the FFT with the Filter H*/ 
int a = alto;
int b = ancho/2 +1; // The reason for the second dimension to have this value is that when the FFT calculation of a real image is performed only the non-redundants outputs are calculated, that’s the reason for the output of the FFT and the filter ‘H’ to be equal. 
// Matrix point-by-point multiplicaction: [axb]*[axb]
fftw_complex* res ; // result will be stored here
res = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*a*b);
res = multiply_matrix_2D(out,H, a, b);

问题位于这里,在函数' multiply_matrix_2D '内的循环中:

 fftw_complex*  prueba_r01::multiply_matrix_2D(fftw_complex* out, double* H, int M ,int N){
/* The matrix out[MxN] or [n0x(n1/2)+1] is the image after the FFT , and the out_H[MxN] is the filter in the frequency domain,
both are multiplied POINT TO POINT, it has to be called  twice, one for the imaginary part and another for the normal part
*/
fftw_complex *H_cast;
H_cast = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);
H_cast= reinterpret_cast<fftw_complex*> (H); // casting from double* to fftw_complex*
fftw_complex *res; // the result of the multiplication will be stored here
res = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);
//Loop for calculating the matrix point-to-point multiplication
for (int x = 0; x<M ; x++){
    for (int y = 0; y<N ; y++){
            res[x*N+y][0] = out[x*N+y][0]*(H_cast[x*N+y][0]+H_cast[x*N+y][1]); 
            res[x*N+y][1] = out[x*N+y][1]*(H_cast[x*N+y][0]+H_cast[x*N+y][1]); 
        }
}
fftw_free(H_cast);
return res;
}

x = 95, y = 93的值M = 191, N = 96;prueba_r01.exe中0x004273ab异常:0xC0000005访问违规读取0x01274000.

画像http://img846.imageshack.us/img846/4585/accessviolationproblem.png

其中变量的很多值都是红色的,并且对于翻译问题:H_cast[][1]在值框中有:" Error30CXX0000:无法计算表达式"。

我将非常感谢任何形式的帮助,请!!安东尼奥

这部分代码

H_cast = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*M*N);
H_cast= reinterpret_cast<fftw_complex*> (H); // casting from double* to fftw_complex*

首先为H_cast分配一个新的缓冲区,然后立即将其设置为指向原来的H。它不复制数据,只复制指针。

在函数结束时,一些缓冲区被释放

fftw_free(H_cast);

似乎释放了H所指向的数据,而不是在函数中分配的缓冲区。

当回到调用者时,那里的H丢失了!

ITK内部有一个FFT类,可以使用cmake中的fftw (USE_FFTW)进行配置。这个类描述了如何从fftw中引用ITK原始缓冲区内存。

PS:即将发布的ITKv4已经大大提高了fftw的兼容性