Exception: std::bad_alloc at memory location ... with SparseMat

asked 2018-03-28 14:19:52 -0600

int mat_size_o = 60 * 60 * 60;

int sz[] = { 10000, mat_size_o };

SparseMat mem1(2, sz, CV_8UC1);

int idx[2];

for (int i = 0; i < 60; ++i) {
    for (int j = 0; j < 60; ++j) {
        for (int k = 0; k < 60; ++k) {
            int IOut = 60 * 60 * i + 60 * j + k;
            Out = calc_sum(IOut);
            Outp.at<float>(i, j, k) = Out;
        }
    }
}
float calc_sum(int IOut) {
int Sum0 = 0;
float Sum = 0;
int IIn0;
int IIn;
uchar Char;
for (int i = 0; i < nrows_i; ++i) { 
    IIn0 = ncols_i * i;
    for (int j = 0; j < ncols_i; ++j) {
        IIn = IIn0 + j;
        idx[0] = IIn;
        idx[1] = IOut;
        Char = mem1.ref<uchar>(idx);

        Sum0 += Char;
    }
}
Sum = Sum0;
return Sum;
}

Generates exception at i=0, j=4, k=59; For another matrix at i=0, j=6, k=59 It happens inside calc_sum() at Char = mem1.ref<uchar>(idx); when i = 43, j = 3 (nrows_i = ncols_i = 100). Initially I mixed int with unsigned int sometimes, but correction doesn't help. Did anybody encounter such errors?

edit retag flag offensive close merge delete

Comments

1

Not really an answer... but the only time I've ever seen bad_alloc is when memory is running low.

sjhalayka gravatar imagesjhalayka ( 2018-03-28 15:42:37 -0600 )edit

Useful hint, indeed. Especially if I use debugger and run several programs, but the error remains even if I exit Visual Studio and run standalone EXE file. Probably this is something similar to General Protection Fault (access violation)? This may happen during type mixing or errors in addressing mode.

ya_ocv_user gravatar imageya_ocv_user ( 2018-03-29 00:19:19 -0600 )edit

This answer also suggests that the cause is memory shortage rather than access violation.

ya_ocv_user gravatar imageya_ocv_user ( 2018-03-29 00:35:51 -0600 )edit

Also using SparseMat drastically reduces speed of calculations.

ya_ocv_user gravatar imageya_ocv_user ( 2018-03-29 01:52:33 -0600 )edit