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
This commit is contained in:
ejakowatz
2004-08-24 23:54:02 +00:00
parent 968aa8c895
commit 72a804515c
3 changed files with 39 additions and 17 deletions
+13 -10
View File
@@ -125,17 +125,20 @@ status_t BMessageBody::AddData(const char *name, const T1 &data, type_code type)
status_t err = B_OK; status_t err = B_OK;
BMessageField* BMF = FindData(name, type, err); BMessageField* BMF = FindData(name, type, err);
if (err == B_NAME_NOT_FOUND) if (err)
{ {
// Reset err; we'll create the field if (err == B_NAME_NOT_FOUND)
err = B_OK; {
} // Reset err; we'll create the field
else err = B_OK;
{ }
// Looking for B_BAD_TYPE here in particular, which would indicate else
// that we tried to add data of type X when we already had data of {
// type Y with the same name // Looking for B_BAD_TYPE here in particular, which would indicate
return err; // 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) if (!BMF)
+8 -6
View File
@@ -375,14 +375,16 @@ const void*
BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>:: BMessageFieldImpl<T1, StoragePolicy, SizePolicy, PrintPolicy, FlattenPolicy, GetDataPolicy>::
DataAt(int32 index, ssize_t* size) const DataAt(int32 index, ssize_t* size) const
{ {
if (index > CountItems()) if (index < CountItems())
return NULL; {
*size = SizePolicy::Size(fData[index]);
const T1& ref = fData[index];
const T1* data = &ref;
*size = SizePolicy::Size(fData[index]); return GetDataPolicy::GetData(data);//(const void*)data;
const T1& ref = fData[index]; }
const T1* data = &ref;
return GetDataPolicy::GetData(data);//(const void*)data; return NULL;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template template
+18 -1
View File
@@ -55,6 +55,7 @@ BMessageBody::BMessageBody(const BMessageBody &rhs)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BMessageBody::~BMessageBody() BMessageBody::~BMessageBody()
{ {
MakeEmpty();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
BMessageBody& BMessageBody::operator=(const BMessageBody &rhs) 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) 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() status_t BMessageBody::MakeEmpty()
{ {
for (TMsgDataMap::iterator i = fData.begin();
i != fData.end();
++i)
{
delete i->second;
}
fData.clear(); fData.clear();
return B_OK; return B_OK;
} }