[Solved] Memory Leak in Struct C++ [closed]


First, your code won’t compile with the stray “struct” in there. Deleting that, we can answer your question: it depends how the parameter was allocated, but in any case you should only do it once.

int main()
{
    int a[]  = { 1, 1, 1 };
    int i    = 42;
    int* pi1 = &i;
    int* pi2 = new int[10];
    int* pi3 = new int(42);
    std::vector<int> v( 10 );
    std::shared_ptr<int> sp( new int(42) );

    int* r1 = ramp_output(a);
    int* r2 = ramp_output(&i);
    int* r3 = ramp_output(pi1);
    int* r4 = ramp_output(pi2);
    int* r5 = ramp_output(pi3);
    int* r6 = ramp_output(&v[0]);
    int* r7 = ramp_output(sp.get());

    // what to dealloc? Only these
    delete [] pi2; // or delete [] r4; but not both!
    delete pi3;    // or delete    r5; but not both!
}

All the others clean up after themselves one way or another.

1

solved Memory Leak in Struct C++ [closed]