Core/DBLayer: Fix a memory leak in Field class, properly clear buffer before repopulating it.

Fixes many issues (from wrong subnames on NPCs to absent GO/Creature spawns)
Thanks to Rat for helping find the issue

--HG--
branch : trunk
This commit is contained in:
Machiavelli
2010-09-25 13:12:12 +02:00
parent 0fa7d24637
commit 477b30a1ad
3 changed files with 11 additions and 5 deletions
+8 -2
View File
@@ -32,10 +32,13 @@ Field::~Field()
void Field::SetByteValue(const void* newValue, const size_t newSize, enum_field_types newType, uint32 length) void Field::SetByteValue(const void* newValue, const size_t newSize, enum_field_types newType, uint32 length)
{ {
if (data.value)
CleanUp();
// This value stores raw bytes that have to be explicitly casted later // This value stores raw bytes that have to be explicitly casted later
if (newValue) if (newValue)
{ {
data.value = new char [newSize]; data.value = new char[newSize];
memcpy(data.value, newValue, newSize); memcpy(data.value, newValue, newSize);
data.length = length; data.length = length;
} }
@@ -43,8 +46,11 @@ void Field::SetByteValue(const void* newValue, const size_t newSize, enum_field_
data.raw = true; data.raw = true;
} }
void Field::SetStructuredValue(char* newValue, enum_field_types newType, const size_t newSize) void Field::SetStructuredValue(char* newValue, enum_field_types newType)
{ {
if (data.value)
CleanUp();
// This value stores somewhat structured data that needs function style casting // This value stores somewhat structured data that needs function style casting
if (newValue) if (newValue)
{ {
+2 -2
View File
@@ -249,11 +249,11 @@ class Field
} data; } data;
void SetByteValue(const void* newValue, const size_t newSize, enum_field_types newType, uint32 length); void SetByteValue(const void* newValue, const size_t newSize, enum_field_types newType, uint32 length);
void SetStructuredValue(char* newValue, enum_field_types newType, const size_t newSize); void SetStructuredValue(char* newValue, enum_field_types newType);
void CleanUp() void CleanUp()
{ {
delete[] (data.value); delete[] ((char*)data.value);
data.value = NULL; data.value = NULL;
} }
+1 -1
View File
@@ -162,7 +162,7 @@ bool ResultSet::NextRow()
} }
for (uint32 i = 0; i < m_fieldCount; i++) for (uint32 i = 0; i < m_fieldCount; i++)
m_currentRow[i].SetStructuredValue(row[i], m_fields[i].type, Field::SizeForType(&m_fields[i])); m_currentRow[i].SetStructuredValue(row[i], m_fields[i].type);
return true; return true;
} }