program crashes "occasionally"

Dear all,

I have a weird problem. Each time I run my program, sometimes it runs successfully, but sometimes it crashes, with the error message:

"cppProject(4255,0x7fff730ef300) malloc: *** error for object 0x1003008c8: incorrect checksum for freed object - object was probably modified after being freed."

I think each time I run (re-compile and re-run) the program, it should yield the same results, either correct or incorrect, but it should be consistent. Why different run returns different results? (I run the program 10 times for example, then maybe 2 times it will return the error).

Some more details:
I am using Eigen library for some matrix manipulation. Since I am trying to write code as generic as possible, I use dynamic matrix allocation, something like MatrixXd matA(A.cols(), A.rows());. If I use static allocation, like Matrix3d matA(3,3);, then there's problem.

I am using Xcode.

Thanks.
Last edited on
Somewhere in your program you are handling dynamic memory incorrectly. Sometimes your program is lucky and acquires a portion of memory that allows it to be successful, but other times it acquires a portion of memory that leads to failure. Is your code small enough to share?
Hi kevinkj2000,

Thanks for your reply.

I am not handling any of dynamic allocation, I am simply using Eigen library, which involves some dynamic matrix manipulation. It's a rather complex library so I really have no idea to debug the library.

Below are some snippet of my code, but as I said, the main algorithm is implemented by Eigen, not myself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template<typename T1, typename T2, typename T3, typename T4, typename T5>
    void response(const T1& matA, const T2& matB, const T1& matQ,
                  const T3& matR, vector<T4>& x, vector<T5>& u)
    {
        // find the Kalman gain
        T1 areSolu = dare(matA, matB, matQ, matR);      // ARE solution

        // following code occasionally causes problems, since it is dynamic allocation
//        MatrixXd temp(matB.cols(), matB.cols());
//        temp = matB.transpose()*areSolu*matB + matR;
//        MatrixXd kalmGain(matB.cols(), matB.rows());
//        kalmGain = temp.inverse() * matB.transpose() * areSolu * matA;
        
        // following code has no issue, with static allocation
        Matrix<double, 2, 2> temp;
        temp = matB.transpose()*areSolu*matB + matR;
        Matrix<double, 2, 4> kalmGain;
        kalmGain = temp.inverse() * matB.transpose() * areSolu * matA;
        
        // system response
        for (size_t k = 0; k != x.size(); ++k) {
            u[k] = -kalmGain * x[k];
            x[k+1] = matA*x[k] + matB*u[k];
        }

    }   // end response() 
x[k+1] on line 23 goes out of bounds when k is x.size()-1
You should add a restraint checking that k<u.size() on line 21, unless you always know that u and x are always the same length.
kevinkjt2000,

Thanks for the reply. Yes I change the dimension of u to be the same size of x, now the problem solved.
Topic archived. No new replies allowed.