Search code examples
pythonc++memoryboostmemory-management

boost::python memory error with dynamic array and delete operator


I have this code that I'm using to test the speed of the C++ code vs. the original python code (I used a pointer array to make sure that the data is in a single continous block and because a vector has to make copies when it grows, which takes O(N) time):

void MyClass::test_packet(){
    time_t t1;
    time(&t1);
    PacketInfo* packets;
    for(int i = 0; i < 336000; i++){
        for(int j = 0; j < 18; j++){
            int N = 18;
            // allocate memory for 18 objects in a continous block
            packets = new PacketInfo[N];
            // create the packetInfo objects
            packets[i] = PacketInfo(i, i);
            // free the 18 memory blocks
            delete[] packets;
        }
    }

    time_t t2;
    time(&t2);
    cout << "total time for 336000 * 18 packets : " << (t2 - t1) << "seconds" << endl;
}
BOOST_PYTHON_MODULE(MyClass)
{
    class_<MyClass>("MyClass", init< /*parameter types go here */>())
        // some other functions
        .def("test", &MyClass::test_packet);
}

the python test file looks like this:

from MyClass import *
MyClass.test()

this gave me a double free or corrupt memory error:

*** Error in `python3': double free or corruption (!prev): 0x00000000016b1b10 ***

I commented the delete[] operator, but that gave me a segmentation fault:

Erreur de segmentation (core dumped)

Any idea how I can fix this?

Thanks


Solution

  • Some incorrect code here

    for(int i = 0; i < 336000; i++){
        for(int j = 0; j < 18; j++){
            int N = 18;
            // allocate memory for 18 objects in a continous block
            packets = new PacketInfo[N];
            // create the packetInfo objects
            packets[i] = PacketInfo(i, i);
    

    If i is greater than 18 (which obviously it will be) then packets[i] will be an out of bounds array access.

    The alternative with packets[j] doesn't make much more sense, since the allocation is positioned incorrectly with respect to the loop (presumably it should be before the loop).

    Plus your statement about vectors is incorrect.

    vector<PacketInfo> packets(18);
    

    will allocate a vector of size 18, with 18 continuous elements, and since the vector is not growing no reallocation either.

    Looking at your comments in the code I think the code you meant to write is

    for(int i = 0; i < 336000; i++){
        int N = 18;
        // allocate memory for 18 objects in a continous block
        vector<PacketInfo> packets(N);
        for(int j = 0; j < N; j++){
            // create the packetInfo objects
            packets[i] = PacketInfo(i, i);
        }
    }