Possible Memory Leak?

closed account (zb0S216C)
I'm trying to figure out whether or not this code causes a memory leak:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char *Buffer( new char[100] );
char *End( Buffer + 99 );

Object *Last( 0 );

Last = new( Buffer ) Object( );
++Last; // Leak after the semi-colon sequence point?
Last = new( Buffer ) Object( );

// Delete...
Last->~Object( );
--Last;
Last->~Object( );
Last = 0;

Notes: Object is a dummy structure for testing.

Does Last cause a leak when it's incremented?

Wazzak
No, modifying your Last pointer doesn't leak memory because buffer is keeping track of the memory. You're using "placement new" which doesn't allocate memory; it only causes an Object object to be constructed at the location pointed-by Last.

what will cause a leak is that you haven't called delete [] on the memory you've allocated with new []. So while you may have called a destructor, you still haven't released the memory pointed by buffer


edit Although you still have undefined behaviour, because you're overwriting the first object which you constructed with another object. Perhaps you intended to do something like this?
1
2
3
4
5
6
7
8
9
10
11
    char* Buffer( new char[100] );
    Object* Last = reinterpret_cast<Object*>( Buffer );

    Last = new( Last ) Object;
    ++Last;
    Last = new( Last ) Object;

    Last->~Object();
    --Last;
    Last->~Object();
    delete [] Buffer;
Last edited on
closed account (zb0S216C)
Thanks for your reply, Bench82. You've pretty much answered everything. :)

Bench82 wrote:
However, you later have undefined behaviour because you're calling your Object constructor on the same bit of memory twice.

Can you point that out within my code?

Bench82 wrote:
what will cause a leak is that you haven't called delete [] on the memory you've allocated with new [].

Oh, I know that, I just omitted it since it wasn't within the scope of my question. :)

Thanks again, Bench82.

Edit:

This is what I initially thought: Buffer allocates 100 1 byte blocks. Object is, let's say 4 bytes, in length. When Last is incremented, it moved 4 bytes past its initial point. Am I wrong?

Wazzak
Last edited on
@Framework - sorry for the multiple edits - see the latest update to my post (I had a few unnecessary reinterpret_cast<>s in there before and it got a bit ugly!).

If you use the (Last) pointer as the parameter to placement new instead of (Buffer), the objects should land in the place where you intended to put them. If you're using Last to keep a track of the next available bit of memory, then the only time you need Buffer is delete[].



Framework wrote:
This is what I initially thought: Buffer allocates 100 1 byte blocks. Object is, let's say 4 bytes, in length. When Last is incremented, it moved 4 bytes past its initial point. Am I wrong?
You're spot on there - however, you were passing (Buffer) as your argument to placement new, so both objects were constructed at the (Buffer) position instead of the (Last) position. Apologies for the confusion caused by my post editing :)
Last edited on
closed account (zb0S216C)
OK, so basically, by casting Buffer to Object *, I'm effectively diving the 100 bytes of Buffer's memory into 4 byte blocks?

Bench82 wrote:
You're spot on there - however, you were passing (Buffer) as your argument to placement new, so both objects were constructed at the (Buffer) position instead of the (Last) position.

Ah, I see it now. :)

Bench82 wrote:
Apologies for the confusion caused by my post editing :)

No worries :)

Wazzak
Topic archived. No new replies allowed.