Skip to content

Commit b8d86bd

Browse files
committed
fix from memory allocation: new instead of malloc.
1 parent abe9680 commit b8d86bd

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

include/cppcore/Common/Variant.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,16 @@ class Variant {
193193
};
194194

195195
inline Variant::Variant() :
196-
m_Type(None), m_BufferSize(0), m_pData(nullptr) {
196+
m_Type(None),
197+
m_BufferSize(0),
198+
m_pData(nullptr) {
197199
// empty
198200
}
199201

200202
inline Variant::Variant(Type type, void *pData, size_t numItems) :
201-
m_Type(None), m_BufferSize(0), m_pData(nullptr) {
203+
m_Type(None),
204+
m_BufferSize(0),
205+
m_pData(nullptr) {
202206
if (isValid(type, numItems)) {
203207
size_t size = 0;
204208
m_Type = type;
@@ -214,13 +218,17 @@ inline Variant::Variant(Type type, void *pData, size_t numItems) :
214218
}
215219

216220
inline Variant::Variant(bool value) :
217-
m_Type(Boolean), m_BufferSize(0), m_pData(nullptr) {
221+
m_Type(Boolean),
222+
m_BufferSize(0),
223+
m_pData(nullptr) {
218224
reserve(Boolean, 0);
219225
::memcpy(m_pData, &value, m_BufferSize);
220226
}
221227

222228
inline Variant::Variant(const Variant &other) :
223-
m_Type(None), m_BufferSize(0), m_pData(NULL) {
229+
m_Type(None),
230+
m_BufferSize(0),
231+
m_pData(nullptr) {
224232
m_Type = other.m_Type;
225233
if (String == m_Type) {
226234
setStdString(other.getString());
@@ -354,9 +362,14 @@ inline float *Variant::getFloat4x4() const {
354362
inline void Variant::setStdString(const std::string &value) {
355363
clear();
356364
m_Type = String;
357-
m_pData = new char[value.size() + 1];
365+
if (value.empty()) {
366+
m_pData = nullptr;
367+
m_BufferSize = 0;
368+
}
369+
m_BufferSize = sizeof(char) * (value.size() + 1);
370+
m_pData = (char*) ::malloc(m_BufferSize);
358371
::memcpy(m_pData, value.c_str(), sizeof(char) * value.size());
359-
char *ptr = (char *)m_pData;
372+
char *ptr = (char *) m_pData;
360373
ptr[value.size()] = '\0';
361374
}
362375

0 commit comments

Comments
 (0)