From 72a804515cb1366c193794d01630715053ec78d3 Mon Sep 17 00:00:00 2001 From: ejakowatz Date: Tue, 24 Aug 2004 23:54:02 +0000 Subject: [PATCH] Fixed a couple of memory leaks and an incorrect index bounds check. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8635 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/app/MessageBody.h | 23 +++++++++++++---------- headers/private/app/MessageField.h | 14 ++++++++------ src/kits/app/MessageBody.cpp | 19 ++++++++++++++++++- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/headers/private/app/MessageBody.h b/headers/private/app/MessageBody.h index b68143f40f..4a1a0a904d 100644 --- a/headers/private/app/MessageBody.h +++ b/headers/private/app/MessageBody.h @@ -125,17 +125,20 @@ status_t BMessageBody::AddData(const char *name, const T1 &data, type_code type) status_t err = B_OK; BMessageField* BMF = FindData(name, type, err); - if (err == B_NAME_NOT_FOUND) + if (err) { - // Reset err; we'll create the field - err = B_OK; - } - else - { - // Looking for B_BAD_TYPE here in particular, which would indicate - // that we tried to add data of type X when we already had data of - // type Y with the same name - return err; + if (err == B_NAME_NOT_FOUND) + { + // Reset err; we'll create the field + err = B_OK; + } + else + { + // Looking for B_BAD_TYPE here in particular, which would indicate + // that we tried to add data of type X when we already had data of + // type Y with the same name + return err; + } } if (!BMF) diff --git a/headers/private/app/MessageField.h b/headers/private/app/MessageField.h index 9a5b94ef7f..1f576e543c 100644 --- a/headers/private/app/MessageField.h +++ b/headers/private/app/MessageField.h @@ -375,14 +375,16 @@ const void* BMessageFieldImpl:: DataAt(int32 index, ssize_t* size) const { - if (index > CountItems()) - return NULL; + if (index < CountItems()) + { + *size = SizePolicy::Size(fData[index]); + const T1& ref = fData[index]; + const T1* data = &ref; - *size = SizePolicy::Size(fData[index]); - const T1& ref = fData[index]; - const T1* data = &ref; + return GetDataPolicy::GetData(data);//(const void*)data; + } - return GetDataPolicy::GetData(data);//(const void*)data; + return NULL; } //------------------------------------------------------------------------------ template diff --git a/src/kits/app/MessageBody.cpp b/src/kits/app/MessageBody.cpp index 6773c8c401..76434d5980 100644 --- a/src/kits/app/MessageBody.cpp +++ b/src/kits/app/MessageBody.cpp @@ -55,6 +55,7 @@ BMessageBody::BMessageBody(const BMessageBody &rhs) //------------------------------------------------------------------------------ BMessageBody::~BMessageBody() { + MakeEmpty(); } //------------------------------------------------------------------------------ BMessageBody& BMessageBody::operator=(const BMessageBody &rhs) @@ -294,11 +295,27 @@ status_t BMessageBody::RemoveData(const char* name, int32 index) //------------------------------------------------------------------------------ status_t BMessageBody::RemoveName(const char* name) { - return fData.erase(name) ? B_OK : B_NAME_NOT_FOUND; + TMsgDataMap::iterator i = fData.find(name); + if (i == fData.end()) + { + return B_NAME_NOT_FOUND; + } + + delete i->second; + fData.erase(i); + + return B_OK; } //------------------------------------------------------------------------------ status_t BMessageBody::MakeEmpty() { + for (TMsgDataMap::iterator i = fData.begin(); + i != fData.end(); + ++i) + { + delete i->second; + } + fData.clear(); return B_OK; }