[ADMB Users] The slow destruction of ADMB

dave fournier otter at otter-rsch.com
Sun Jan 1 08:17:18 PST 2012


I was getting strange behaviour (canadian spelling)  from a
routine. I traced it to a buggy routine in  ivsort.cpp  which seems to have
replaced my original code.


    int *intarray;
    intarray = new int[size];
    int i;
    for(i=0;i<size;i++)
    {
       intarray[i] = v(lb+i);
    }

    //  .......

    delete intarray;

What is wrong with that?

Two things. If you allocate memory with new int[size]
you need to deallocate it with

    delete [] intarray;

Second you should not do this anyway. New and delete are really error prone
so you should use an ivector so that the destructor gets called 
automatically.


    ivector intarray(0,size-1);
    int i;
    for(i=0;i<size;i++)
    {
       intarray[i] = v(lb+i);
    }

Now you get bounds checking in the safe version of the code.

Whenever you make a change to the code you should run it through valgrind.
With much more of this all of admb will be buggy and waste ones time with
strange failures.







More information about the Users mailing list