Skip to content
Browse files

Memory cleanup in case of construction error

  • Loading branch information...
1 parent 14561dd commit e613202d19fc9bc787d28bb614d6ae028f29ed91 @mariano committed Jun 3, 2011
Showing with 26 additions and 16 deletions.
  1. +25 −16 src/result.cc
  2. +1 −0 src/result.h
View
41 src/result.cc
@@ -76,29 +76,38 @@ node_db_drizzle::Result::Result(drizzle_st* drizzle, drizzle_result_st* result)
throw node_db::Exception("Could not buffer columns");
}
- this->totalColumns = drizzle_result_column_count(this->result);
- if (this->totalColumns > 0) {
- this->empty = false;
- this->columns = new Column*[this->totalColumns];
- if (this->columns == NULL) {
- throw node_db::Exception("Could not allocate storage for columns");
- }
+ try {
+ this->totalColumns = drizzle_result_column_count(this->result);
+ if (this->totalColumns > 0) {
+ this->empty = false;
+ this->columns = new Column*[this->totalColumns];
+ if (this->columns == NULL) {
+ throw node_db::Exception("Could not allocate storage for columns");
+ }
- uint16_t i = 0;
- drizzle_column_st *current;
- while ((current = drizzle_column_next(this->result)) != NULL) {
- this->columns[i++] = new Column(current);
- if (this->columns[i] == NULL) {
- delete [] this->columns;
- throw node_db::Exception("Could not allocate storage for column");
+ uint16_t i = 0;
+ drizzle_column_st *current;
+ while ((current = drizzle_column_next(this->result)) != NULL) {
+ this->columns[i++] = new Column(current);
+ if (this->columns[i] == NULL) {
+ this->totalColumns = i;
+ throw node_db::Exception("Could not allocate storage for column");
+ }
}
- }
- this->nextRow = this->row();
+ this->nextRow = this->row();
+ }
+ } catch(...) {
+ this->free();
+ throw;
}
}
node_db_drizzle::Result::~Result() {
+ this->free();
+}
+
+void node_db_drizzle::Result::free() throw() {
if (this->columns != NULL) {
for (uint16_t i = 0; i < this->totalColumns; i++) {
delete this->columns[i];
View
1 src/result.h
@@ -46,6 +46,7 @@ class Result : public node_db::Result {
bool empty;
char** row() throw(node_db::Exception&);
+ void free() throw();
private:
drizzle_st *drizzle;

0 comments on commit e613202

Please sign in to comment.
Something went wrong with that request. Please try again.