mismatched free/delete in typeregister.h

David Nolden david.nolden.kdevelop at art-master.de
Mon May 30 14:36:40 UTC 2011


2011/5/30 Milian Wolff <mail at milianw.de>:
> David Nolden, 29.05.2011:
>> 2011/5/28 Christoph Bartoschek <bartoschek at gmx.de>:
>> > Hi
>> >
>> > abstracttype.h:254 has the line
>> >
>> > return *new (new char[size]) DataType(rhs);
>> >
>> > Here one first allocates size bytes with new[]. Into the allocated space
>> > a new DataType object is created by using placement new. Placement new
>> > gets a pointer to a memory location and creates the object there instead
>> > of fetching memory internally. The syntax is:
>> >
>> > new (pointer) T();
>> >
>> > This is also used in typeregister.h. In line 99 however the memory is
>> > freed with
>> >
>> > delete temp;
>> >
>> > Valgrind complains because delete cannot be used for memory that was
>> > allocated with new []. Instead one has to use delete []. Therefore the
>> > correct code in line 99 should be:
>> >
>> > delete [] reinterpret_cast<char *>(temp);
>>
>> This is right, it is deallocated like this in other places, and should
>> be deallocated here in the same way.
>>
>> > The cast is necessary because the memory was allocated as a char array.
>> >
>> > There is one problem with the code: The destructor of the DataType is not
>> > called. Therefore the solution is:
>> >
>> > temp->~Data();
>> >
>> > delete [] reinterpret_cast<char *>(temp);
>>
>> Yes, this call should be added too.
>
> will you fix this or should I do that?
I've already done it.




More information about the KDevelop-devel mailing list