Fixed some memory leaks in case of errors (Add*() only).

Some minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13661 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-07-13 18:25:23 +00:00
parent c9955bf00a
commit b9dddc39f3
+56 -50
View File
@@ -1288,24 +1288,26 @@ DEFINE_LAZY_FIND_FUNCTION(const char* , String)
#undef DEFINE_LAZY_FIND_FUNCTION #undef DEFINE_LAZY_FIND_FUNCTION
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
status_t status_t
BMessage::AddString(const char* name, const char* a_string) BMessage::AddString(const char* name, const char* string)
{ {
return AddData(name, B_STRING_TYPE, a_string, strlen(a_string)+1); return AddData(name, B_STRING_TYPE, string, strlen(string) + 1);
} }
status_t status_t
BMessage::AddString(const char* name, const BString& a_string) BMessage::AddString(const char* name, const BString& string)
{ {
return AddData(name, B_STRING_TYPE, a_string.String(), a_string.Length()+1); return AddData(name, B_STRING_TYPE, string.String(), string.Length() + 1);
} }
status_t status_t
BMessage::AddPointer(const char* name, const void* ptr) BMessage::AddPointer(const char* name, const void* pointer)
{ {
return AddData(name, B_POINTER_TYPE, &ptr, sizeof(ptr)); return AddData(name, B_POINTER_TYPE, &pointer, sizeof(pointer));
} }
@@ -1319,13 +1321,20 @@ BMessage::AddMessenger(const char* name, BMessenger messenger)
status_t status_t
BMessage::AddRef(const char* name, const entry_ref* ref) BMessage::AddRef(const char* name, const entry_ref* ref)
{ {
char* buffer = new(nothrow) char[sizeof (entry_ref) + B_PATH_NAME_LENGTH]; char* buffer = new(nothrow) char[sizeof(entry_ref) + B_PATH_NAME_LENGTH];
if (buffer == NULL)
return B_NO_MEMORY;
size_t size; size_t size;
status_t err = entry_ref_flatten(buffer, &size, ref); status_t err = entry_ref_flatten(buffer, &size, ref);
if (!err) { if (err >= B_OK) {
BDataBuffer databuffer((void*)buffer, size); BDataBuffer databuffer((void*)buffer, size);
err = fBody->AddData<BDataBuffer>(name, databuffer, B_REF_TYPE); err = fBody->AddData<BDataBuffer>(name, databuffer, B_REF_TYPE);
} // ToDo: even if the code looks like it, test this for real
// if AddData() fails here, the buffer is freed automatically
// as part of the BDataBuffer destruction
} else
delete[] buffer;
return err; return err;
} }
@@ -1334,39 +1343,40 @@ BMessage::AddRef(const char* name, const entry_ref* ref)
status_t status_t
BMessage::AddMessage(const char* name, const BMessage* msg) BMessage::AddMessage(const char* name, const BMessage* msg)
{ {
status_t err = B_OK;
ssize_t size = msg->FlattenedSize(); ssize_t size = msg->FlattenedSize();
char* buffer = new(nothrow) char[size]; char* buffer = new(nothrow) char[size];
if (buffer) { if (buffer == NULL)
err = msg->Flatten(buffer, size); return B_NO_MEMORY;
if (!err) {
BDataBuffer databuffer((void*)buffer, size); status_t err = msg->Flatten(buffer, size);
err = fBody->AddData<BDataBuffer>(name, databuffer, B_MESSAGE_TYPE); if (err >= B_OK) {
} BDataBuffer dataBuffer((void*)buffer, size);
} else { err = fBody->AddData<BDataBuffer>(name, dataBuffer, B_MESSAGE_TYPE);
err = B_NO_MEMORY; // if AddData() fails here, the buffer is freed automatically
} // as part of the BDataBuffer destruction
} else
delete[] buffer;
return err; return err;
} }
status_t status_t
BMessage::AddFlat(const char* name, BFlattenable* obj, int32 count) BMessage::AddFlat(const char* name, BFlattenable* object, int32 count)
{ {
status_t err = B_OK; ssize_t size = object->FlattenedSize();
ssize_t size = obj->FlattenedSize();
char* buffer = new(nothrow) char[size]; char* buffer = new(nothrow) char[size];
if (buffer) { if (buffer == NULL)
err = obj->Flatten((void*)buffer, size); return B_NO_MEMORY;
if (!err) {
err = AddData(name, obj->TypeCode(), (void*)buffer, size, status_t err = object->Flatten((void*)buffer, size);
obj->IsFixedSize(), count); if (err >= B_OK) {
} BDataBuffer dataBuffer((void*)buffer, size);
err = fBody->AddData<BDataBuffer>(name, dataBuffer, object->TypeCode());
// if AddData() fails here, the buffer is freed automatically
// as part of the BDataBuffer destruction
} else
delete[] buffer; delete[] buffer;
} else {
err = B_NO_MEMORY;
}
return err; return err;
} }
@@ -1376,21 +1386,17 @@ status_t
BMessage::AddData(const char* name, type_code type, const void* data, BMessage::AddData(const char* name, type_code type, const void* data,
ssize_t numBytes, bool is_fixed_size, int32 /*count*/) ssize_t numBytes, bool is_fixed_size, int32 /*count*/)
{ {
/** /** @note
@note Because we're using vectors for our item storage, the count param Because we're using vectors for our item storage, the count param
is no longer useful to us: dynamically adding more items is not is no longer useful to us: dynamically adding more items is not
really a performance issue, so pre-allocating space for objects really a performance issue, so pre-allocating space for objects
gives us no real advantage. gives us no real advantage.
*/ */
// TODO: test // TODO: test
// In particular, we want to see what happens if is_fixed_size == true and // In particular, we want to see what happens if is_fixed_size == true and
// the user attempts to add something bigger or smaller. We may need to // the user attempts to add something bigger or smaller. We may need to
// enforce the size thing. // enforce the size thing.
//--------------------------------------------------------------------------
// voidref suggests creating a BDataBuffer type which we can use here to
// avoid having to specialize BMessageBody::AddData().
//--------------------------------------------------------------------------
BDataBuffer buffer((void*)data, numBytes, true); BDataBuffer buffer((void*)data, numBytes, true);
return fBody->AddData<BDataBuffer>(name, buffer, type); return fBody->AddData<BDataBuffer>(name, buffer, type);
@@ -1419,39 +1425,39 @@ BMessage::MakeEmpty()
status_t status_t
BMessage::FindString(const char* name, const char** str) const BMessage::FindString(const char* name, const char** string) const
{ {
return FindString(name, 0, str); return FindString(name, 0, string);
} }
status_t status_t
BMessage::FindString(const char* name, int32 index, const char** str) const BMessage::FindString(const char* name, int32 index,
const char** string) const
{ {
ssize_t bytes; ssize_t bytes;
return FindData(name, B_STRING_TYPE, index, return FindData(name, B_STRING_TYPE, index,
(const void**)str, &bytes); (const void**)string, &bytes);
} }
status_t status_t
BMessage::FindString(const char* name, BString* str) const BMessage::FindString(const char* name, BString* string) const
{ {
return FindString(name, 0, str); return FindString(name, 0, string);
} }
status_t status_t
BMessage::FindString(const char* name, int32 index, BString* str) const BMessage::FindString(const char* name, int32 index, BString* string) const
{ {
const char* cstr; const char* cstr;
status_t err = FindString(name, index, &cstr); status_t err = FindString(name, index, &cstr);
if (!err) if (err < B_OK)
{ return err;
*str = cstr;
}
return err; *string = cstr;
return B_OK;
} }