Skip to content

Commit 3d7da33

Browse files
committed
Memory cleanup in case of construction error
1 parent 0afd864 commit 3d7da33

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/result.cc

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,37 +76,47 @@ node_db_mysql::Result::Result(MYSQL* connection) throw(node_db::Exception&)
7676
previousRow(NULL),
7777
nextRow(NULL) {
7878
this->result = mysql_store_result(this->connection);
79-
if (result == NULL && mysql_field_count(this->connection) != 0) {
80-
throw node_db::Exception(mysql_error(this->connection));
81-
} else if (result != NULL) {
82-
this->empty = false;
83-
84-
MYSQL_FIELD* fields = mysql_fetch_fields(this->result);
85-
if (fields == NULL) {
86-
throw node_db::Exception("Could not buffer columns");
87-
}
8879

89-
this->totalColumns = mysql_num_fields(this->result);
90-
if (this->totalColumns > 0) {
91-
this->columns = new Column*[this->totalColumns];
92-
if (this->columns == NULL) {
93-
throw node_db::Exception("Could not allocate storage for columns");
80+
try {
81+
if (result == NULL && mysql_field_count(this->connection) != 0) {
82+
throw node_db::Exception(mysql_error(this->connection));
83+
} else if (result != NULL) {
84+
this->empty = false;
85+
86+
MYSQL_FIELD* fields = mysql_fetch_fields(this->result);
87+
if (fields == NULL) {
88+
throw node_db::Exception("Could not buffer columns");
9489
}
9590

96-
for (uint16_t i = 0; i < this->totalColumns; i++) {
97-
this->columns[i] = new Column(fields[i]);
98-
if (this->columns[i] == NULL) {
99-
delete [] this->columns;
100-
throw node_db::Exception("Could not allocate storage for column");
91+
this->totalColumns = mysql_num_fields(this->result);
92+
if (this->totalColumns > 0) {
93+
this->columns = new Column*[this->totalColumns];
94+
if (this->columns == NULL) {
95+
throw node_db::Exception("Could not allocate storage for columns");
96+
}
97+
98+
for (uint16_t i = 0; i < this->totalColumns; i++) {
99+
this->columns[i] = new Column(fields[i]);
100+
if (this->columns[i] == NULL) {
101+
this->totalColumns = i;
102+
throw node_db::Exception("Could not allocate storage for column");
103+
}
101104
}
102105
}
103-
}
104106

105-
this->nextRow = this->row();
107+
this->nextRow = this->row();
108+
}
109+
} catch(...) {
110+
this->free();
111+
throw;
106112
}
107113
}
108114

109115
node_db_mysql::Result::~Result() {
116+
this->free();
117+
}
118+
119+
void node_db_mysql::Result::free() throw() {
110120
if (this->columns != NULL) {
111121
for (uint16_t i = 0; i < this->totalColumns; i++) {
112122
delete this->columns[i];

src/result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Result : public node_db::Result {
4747
bool empty;
4848

4949
char** row() throw(node_db::Exception&);
50+
void free() throw();
5051

5152
private:
5253
MYSQL* connection;

0 commit comments

Comments
 (0)