Skip to content

Commit

Permalink
Fixed memory/handle leaks (#12)
Browse files Browse the repository at this point in the history
Sorry I missed it.
  • Loading branch information
ntfshard authored Dec 13, 2023
1 parent 4073ebb commit 18b02c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/node_mpg123.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,23 @@ Mpg123n::Mpg123n() {
load_equalizer(mh);
mpg123_control_init(mh);

control_generic_loop_data *loop_data = new control_generic_loop_data();
control_generic_loop_data *loop_data = &my_data;
this->data = loop_data;
loop_data->mh = mh;
loop_data->ao = ao;
loop_data->req.data = loop_data;
loop_data->silent = 0;
loop_data->mode = MODE_STOPPED;
loop_data->command = COMMAND_EMPTY;
this->data = loop_data;
} else {
char message[256];
sprintf("Error occured while initializing mpg123: %d", message);
sprintf(message, "Error occured while initializing mpg123: %d", error);
Nan::ThrowError(message);
}
}

Mpg123n::~Mpg123n() {
mpg123_delete(my_data.mh);
}

NAN_METHOD(Mpg123n::New) {
Expand Down Expand Up @@ -117,10 +118,14 @@ void node_mpg123_loop_async (uv_work_t *req) {
//debug("13: Do real play %s", r->arg);
mpg123_control_play(r->mh, r->arg);
break;
case COMMAND_EMPTY:
break; // GCC found that this value are ommted
default:
puts("node_mpg123: got unexpected command in switch-case");
}
if (clean) {
if (r->arg != NULL) {
delete r->arg;
delete [] r->arg;
r->arg = NULL;
}
r->command = COMMAND_EMPTY;
Expand Down Expand Up @@ -155,7 +160,7 @@ void node_mpg123_loop_after (uv_work_t *req) {

char* stringArgToStr(const v8::Local<v8::Value> arg) {
Nan::Utf8String v8Str(arg);
char *cStr = (char*) malloc(strlen(*v8Str) + 1);
char *cStr = new char[strlen(*v8Str) + 1];
strcpy(cStr, *v8Str);
return cStr;
}
Expand Down
4 changes: 3 additions & 1 deletion src/node_mpg123.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct control_generic_loop_data {
fd_set fds;
int mode;
COMMAND command;
char* arg;
char* arg; //TODO: replace by std::string

char silent;

Expand Down Expand Up @@ -69,7 +69,9 @@ class Mpg123n: public Nan::ObjectWrap {
static NAN_METHOD(Jump);
static NAN_METHOD(Volume);
static Nan::Persistent<v8::Function> constructor;

control_generic_loop_data *data;
control_generic_loop_data my_data;
};

} // mpg123n namespace

0 comments on commit 18b02c4

Please sign in to comment.