Skip to content

Commit 967ddee

Browse files
committed
Improve memory management of SQLite3Buffer
Replaced the memory buffer that was managed by the SQLite3Buffer with an instance of the new detail::SQLite3Memory class. This will make the class copyable and moveable. It's now adhering of the rule of zero.
1 parent f905efc commit 967ddee

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

CppSQLite3.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -191,37 +191,28 @@ CppSQLite3Exception::~CppSQLite3Exception()
191191

192192
////////////////////////////////////////////////////////////////////////////////
193193

194-
CppSQLite3Buffer::CppSQLite3Buffer()
195-
{
196-
mpBuf = 0;
197-
}
198-
199-
200-
CppSQLite3Buffer::~CppSQLite3Buffer()
201-
{
202-
clear();
203-
}
204-
205-
206194
void CppSQLite3Buffer::clear()
207195
{
208-
if (mpBuf)
209-
{
210-
sqlite3_free(mpBuf);
211-
mpBuf = 0;
212-
}
213-
196+
mBuf.clear();
214197
}
215198

216199

217200
const char* CppSQLite3Buffer::format(const char* szFormat, ...)
218201
{
219202
clear();
220203
va_list va;
221-
va_start(va, szFormat);
222-
mpBuf = sqlite3_vmprintf(szFormat, va);
223-
va_end(va);
224-
return mpBuf;
204+
try
205+
{
206+
va_start(va, szFormat);
207+
mBuf = detail::SQLite3Memory(szFormat, va);
208+
va_end(va);
209+
return static_cast<const char*>(mBuf.getBuffer());
210+
}
211+
catch(CppSQLite3Exception&)
212+
{
213+
va_end(va);
214+
throw;
215+
}
225216
}
226217

227218

CppSQLite3.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,15 @@ class CppSQLite3Exception
8787
class CppSQLite3Buffer
8888
{
8989
public:
90-
91-
CppSQLite3Buffer();
92-
93-
~CppSQLite3Buffer();
94-
9590
const char* format(const char* szFormat, ...);
9691

97-
operator const char*() { return mpBuf; }
92+
operator const char*() { return static_cast<char const*>(mBuf.getBuffer()); }
9893

9994
void clear();
10095

10196
private:
10297

103-
char* mpBuf;
98+
detail::SQLite3Memory mBuf;
10499
};
105100

106101

0 commit comments

Comments
 (0)