Skip to content

Commit dece787

Browse files
author
John Haley
committed
Clean up callbacks in structs
1 parent 3b9b637 commit dece787

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

generate/templates/partials/field_accessors.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
wrapper->raw->{{ field.name }} = {% if not field.cType | isPointer %}*{% endif %}ObjectWrap::Unwrap<{{ field.cppClassName }}>(value->ToObject())->GetValue();
4343

4444
{% elsif field.isCallbackFunction %}
45+
if (wrapper->{{ field.name }} != NULL) {
46+
delete wrapper->{{ field.name }};
47+
}
48+
4549
if (value->IsFunction()) {
4650
if (!wrapper->raw->{{ field.name }}) {
4751
wrapper->raw->{{ field.name }} = ({{ field.cType }}){{ field.name }}_cppCallback;

generate/templates/templates/struct_content.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,19 @@ using namespace std;
4141
}
4242

4343
{{ cppClassName }}::~{{ cppClassName }}() {
44-
// This is going to cause memory leaks. We'll have to solve that later
45-
// TODO: Clean up memory better
44+
{% each fields|fieldsInfo as field %}
45+
{% if not field.ignore %}
46+
{% if not field.isEnum %}
47+
{% if field.isCallbackFunction %}
48+
if (this->{{ field.name }} != NULL) {
49+
delete this->{{ field.name }};
50+
this->raw->{{ fields|payloadFor field.name }} = NULL;
51+
}
52+
{% endif %}
53+
{% endif %}
54+
{% endif %}
55+
{% endeach %}
56+
4657
if (this->selfFreeing) {
4758
free(this->raw);
4859
}
@@ -65,7 +76,7 @@ void {{ cppClassName }}::ConstructFields() {
6576
// the current instance
6677
this->raw->{{ field.name }} = NULL;
6778
this->raw->{{ fields|payloadFor field.name }} = (void *)this;
68-
this->{{ field.name }} = new NanCallback();
79+
this->{{ field.name }} = NULL;
6980
{% elsif field.payloadFor %}
7081

7182
Local<Value> {{ field.name }} = NanUndefined();

0 commit comments

Comments
 (0)