mismatched free/delete in typeregister.h

Christoph Bartoschek bartoschek at gmx.de
Sat May 28 08:34:07 UTC 2011


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);

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);

In my opinion it is also ugly to return a reference in copyDataDirectly. It 
does not make clear that one has to delete the object returned.

It is also extremely dangerous to create objects in memory that has been 
fetched wit h new char [].  The alignment might not fit to the object that is 
later constructed in it and one gets really hard to debug errors.

Christoph

Am Freitag 27 Mai 2011 schrieb Milian Wolff:
> Here is a valgrind warning I just noticed. Quite deep inside the DUChain
> code - David, do you happen to know more on how to handle this warning?
> 
> Or could someone else explain me the custom new calls in typeregister.h's
> copy and abstracttype.h's copyDataDirectly ?
> 
> ==16229== Mismatched free() / delete / delete []
> ==16229==    at 0x4C27FFF: operator delete(void*)
> (vg_replace_malloc.c:387) ==16229==    by 0xC84E46D:
> KDevelop::TypeFactory<KDevelop::FunctionType,
> KDevelop::FunctionTypeData>::copy(KDevelop::AbstractTypeData const&,
> KDevelop::AbstractTypeData&, bool) const (typeregister.h:99)
> ==16229==    by 0xC84A875:
> KDevelop::TypeSystem::copy(KDevelop::AbstractTypeData const&,
> KDevelop::AbstractTypeData&, bool) const (typeregister.cpp:58)
> ==16229==    by 0xC85ED28:
> KDevelop::AbstractTypeDataRequest::createItem(KDevelop::AbstractTypeData*)
> const (typerepository.cpp:50)
> ==16229==    by 0xC860F51: KDevelop::Bucket<KDevelop::AbstractTypeData,
> KDevelop::AbstractTypeDataRequest, true,
> 0u>::index(KDevelop::AbstractTypeDataRequest const&, unsigned int)
> (itemrepository.h:641)
> ==16229==    by 0xC85F65E:
> KDevelop::ItemRepository<KDevelop::AbstractTypeData,
> KDevelop::AbstractTypeDataRequest, true, true, 0u,
> 1048576u>::index(KDevelop::AbstractTypeDataRequest const&)
> (itemrepository.h:1440)
> ==16229==    by 0xC85E7D0:
> KDevelop::TypeRepository::indexForType(TypePtr<KDevelop::AbstractType>)
> (typerepository.cpp:104)
> ==16229==    by 0xC84B29A: KDevelop::AbstractType::indexed() const
> (abstracttype.cpp:101)
> ==16229==    by 0xC795F9C:
> KDevelop::DUContext::findLocalDeclarationsInternal(KDevelop::Identifier
> const&, KDevelop::CursorInRevision const&, TypePtr<KDevelop::AbstractType>
> const&, KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const::Checker::check(KDevelop::Declaration*) (ducontext.cpp:680)
> ==16229==    by 0xC7965A3:
> KDevelop::DUContext::findLocalDeclarationsInternal(KDevelop::Identifier
> const&, KDevelop::CursorInRevision const&, TypePtr<KDevelop::AbstractType>
> const&, KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const (ducontext.cpp:726)
> ==16229==    by 0x33957666:
> Cpp::CppDUContext<KDevelop::DUContext>::findLocalDeclarationsInternal(KDev
> elop::Identifier const&, KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const (cppducontext.h:390)
> ==16229==    by 0xC796A1A:
> KDevelop::DUContext::findDeclarationsInternal(KDevVarLengthArray<KSharedPt
> r<KDevelop::DUContext::SearchItem>, 256> const&,
> KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&, KDevelop::TopDUContext
> const*, QFlags<KDevelop::DUContext::SearchFlag>, unsigned int) const
> (ducontext.cpp:767)
> ==16229==    by 0x33956EF2:
> Cpp::CppDUContext<KDevelop::DUContext>::findDeclarationsInternal(KDevVarLe
> ngthArray<KSharedPtr<KDevelop::DUContext::SearchItem>, 256> const&,
> KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&, KDevelop::TopDUContext
> const*, QFlags<KDevelop::DUContext::SearchFlag>, unsigned int) const
> (cppducontext.h:281)
> ==16229==    by 0x339BFC3D: Cpp::FindDeclaration::closeIdentifier(bool)
> (cppducontext.cpp:228)
> ==16229==    by 0x3395AF68:
> Cpp::CppDUContext<KDevelop::DUContext>::findDeclarationsInternal(KDevelop:
> :QualifiedIdentifier const&, KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const (cppducontext.h:363)
> ==16229==    by 0x3395704B:
> Cpp::CppDUContext<KDevelop::DUContext>::findDeclarationsInternal(KDevVarLe
> ngthArray<KSharedPtr<KDevelop::DUContext::SearchItem>, 256> const&,
> KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&, KDevelop::TopDUContext
> const*, QFlags<KDevelop::DUContext::SearchFlag>, unsigned int) const
> (cppducontext.h:286)
> ==16229==    by 0xC797961:
> KDevelop::DUContext::findDeclarations(KDevelop::QualifiedIdentifier
> const&, KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&, KDevelop::TopDUContext const*,
> QFlags<KDevelop::DUContext::SearchFlag>) const (ducontext.cpp:857)
> ==16229==    by 0x33963808: DeclarationBuilder::applyFunctionSpecifiers()
> (declarationbuilder.cpp:1564)
> ==16229==    by 0x3395DF6A:
> DeclarationBuilder::visitDeclarator(DeclaratorAST*)
> (declarationbuilder.cpp:354)
> ==16229==    by 0x3394ACE2:
> ContextBuilder::visitInitDeclarator(InitDeclaratorAST*)
> (contextbuilder.cpp:891)
> ==16229==    by 0x3395D517:
> DeclarationBuilder::visitInitDeclarator(InitDeclaratorAST*)
> (declarationbuilder.cpp:230)
> ==16229==    by 0x33C6E66B: Visitor::visit(AST*) (visitor.cpp:114)
> ==16229==    by 0x3394A403:
> ContextBuilder::visitFunctionDeclaration(FunctionDefinitionAST*)
> (contextbuilder.cpp:606)
> ==16229==    by 0x33991411:
> TypeBuilder::visitFunctionDeclaration(FunctionDefinitionAST*)
> (typebuilder.cpp:534)
> ==16229==    by 0x3395D0FD:
> DeclarationBuilder::visitFunctionDeclaration(FunctionDefinitionAST*)
> (declarationbuilder.cpp:164)
> ==16229==  Address 0x370f7610 is 0 bytes inside a block of size 40 alloc'd
> ==16229==    at 0x4C28658: operator new[](unsigned long)
> (vg_replace_malloc.c:305)
> ==16229==    by 0xC84E544: KDevelop::FunctionTypeData&
> KDevelop::AbstractType::copyDataDirectly<KDevelop::FunctionTypeData>(KDeve
> lop::FunctionTypeData const&) (abstracttype.h:254)
> ==16229==    by 0xC84E3F1: KDevelop::TypeFactory<KDevelop::FunctionType,
> KDevelop::FunctionTypeData>::copy(KDevelop::AbstractTypeData const&,
> KDevelop::AbstractTypeData&, bool) const (typeregister.h:94)
> ==16229==    by 0xC84A875:
> KDevelop::TypeSystem::copy(KDevelop::AbstractTypeData const&,
> KDevelop::AbstractTypeData&, bool) const (typeregister.cpp:58)
> ==16229==    by 0xC85ED28:
> KDevelop::AbstractTypeDataRequest::createItem(KDevelop::AbstractTypeData*)
> const (typerepository.cpp:50)
> ==16229==    by 0xC860F51: KDevelop::Bucket<KDevelop::AbstractTypeData,
> KDevelop::AbstractTypeDataRequest, true,
> 0u>::index(KDevelop::AbstractTypeDataRequest const&, unsigned int)
> (itemrepository.h:641)
> ==16229==    by 0xC85F65E:
> KDevelop::ItemRepository<KDevelop::AbstractTypeData,
> KDevelop::AbstractTypeDataRequest, true, true, 0u,
> 1048576u>::index(KDevelop::AbstractTypeDataRequest const&)
> (itemrepository.h:1440)
> ==16229==    by 0xC85E7D0:
> KDevelop::TypeRepository::indexForType(TypePtr<KDevelop::AbstractType>)
> (typerepository.cpp:104)
> ==16229==    by 0xC84B29A: KDevelop::AbstractType::indexed() const
> (abstracttype.cpp:101)
> ==16229==    by 0xC795F9C:
> KDevelop::DUContext::findLocalDeclarationsInternal(KDevelop::Identifier
> const&, KDevelop::CursorInRevision const&, TypePtr<KDevelop::AbstractType>
> const&, KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const::Checker::check(KDevelop::Declaration*) (ducontext.cpp:680)
> ==16229==    by 0xC7965A3:
> KDevelop::DUContext::findLocalDeclarationsInternal(KDevelop::Identifier
> const&, KDevelop::CursorInRevision const&, TypePtr<KDevelop::AbstractType>
> const&, KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const (ducontext.cpp:726)
> ==16229==    by 0x33957666:
> Cpp::CppDUContext<KDevelop::DUContext>::findLocalDeclarationsInternal(KDev
> elop::Identifier const&, KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const (cppducontext.h:390)
> ==16229==    by 0xC796A1A:
> KDevelop::DUContext::findDeclarationsInternal(KDevVarLengthArray<KSharedPt
> r<KDevelop::DUContext::SearchItem>, 256> const&,
> KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&, KDevelop::TopDUContext
> const*, QFlags<KDevelop::DUContext::SearchFlag>, unsigned int) const
> (ducontext.cpp:767)
> ==16229==    by 0x33956EF2:
> Cpp::CppDUContext<KDevelop::DUContext>::findDeclarationsInternal(KDevVarLe
> ngthArray<KSharedPtr<KDevelop::DUContext::SearchItem>, 256> const&,
> KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&, KDevelop::TopDUContext
> const*, QFlags<KDevelop::DUContext::SearchFlag>, unsigned int) const
> (cppducontext.h:281)
> ==16229==    by 0x339BFC3D: Cpp::FindDeclaration::closeIdentifier(bool)
> (cppducontext.cpp:228)
> ==16229==    by 0x3395AF68:
> Cpp::CppDUContext<KDevelop::DUContext>::findDeclarationsInternal(KDevelop:
> :QualifiedIdentifier const&, KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&,
> KDevelop::TopDUContext const*, QFlags<KDevelop::DUContext::SearchFlag>)
> const (cppducontext.h:363)
> ==16229==    by 0x3395704B:
> Cpp::CppDUContext<KDevelop::DUContext>::findDeclarationsInternal(KDevVarLe
> ngthArray<KSharedPtr<KDevelop::DUContext::SearchItem>, 256> const&,
> KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&,
> KDevVarLengthArray<KDevelop::Declaration*, 40>&, KDevelop::TopDUContext
> const*, QFlags<KDevelop::DUContext::SearchFlag>, unsigned int) const
> (cppducontext.h:286)
> ==16229==    by 0xC797961:
> KDevelop::DUContext::findDeclarations(KDevelop::QualifiedIdentifier
> const&, KDevelop::CursorInRevision const&,
> TypePtr<KDevelop::AbstractType> const&, KDevelop::TopDUContext const*,
> QFlags<KDevelop::DUContext::SearchFlag>) const (ducontext.cpp:857)
> ==16229==    by 0x33963808: DeclarationBuilder::applyFunctionSpecifiers()
> (declarationbuilder.cpp:1564)
> ==16229==    by 0x3395DF6A:
> DeclarationBuilder::visitDeclarator(DeclaratorAST*)
> (declarationbuilder.cpp:354)
> ==16229==    by 0x3394ACE2:
> ContextBuilder::visitInitDeclarator(InitDeclaratorAST*)
> (contextbuilder.cpp:891)
> ==16229==    by 0x3395D517:
> DeclarationBuilder::visitInitDeclarator(InitDeclaratorAST*)
> (declarationbuilder.cpp:230)
> ==16229==    by 0x33C6E66B: Visitor::visit(AST*) (visitor.cpp:114)
> ==16229==    by 0x3394A403:
> ContextBuilder::visitFunctionDeclaration(FunctionDefinitionAST*)
> (contextbuilder.cpp:606)
> ==16229==    by 0x33991411:
> TypeBuilder::visitFunctionDeclaration(FunctionDefinitionAST*)
> (typebuilder.cpp:534)
> ==16229==

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kdevelop-devel/attachments/20110528/6e4f7b82/attachment.html>


More information about the KDevelop-devel mailing list