Commit 2abf518f authored by Jonathan Beck's avatar Jonathan Beck

Rework SWIG interface to handle lifecycle of C pointers correctly.

parent 4f795b01
...@@ -6,7 +6,17 @@ ...@@ -6,7 +6,17 @@
#include <plist/plist.h> #include <plist/plist.h>
typedef struct { typedef struct {
plist_t node; plist_t node;
char should_keep_plist;
} PListNode; } PListNode;
PListNode *allocate_wrapper() {
PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode));
if (wrapper) {
memset(wrapper, 0, sizeof(PListNode));
return wrapper;
}
return NULL;
}
%} %}
%include "stdint.i" %include "stdint.i"
...@@ -27,7 +37,6 @@ typedef enum { ...@@ -27,7 +37,6 @@ typedef enum {
} plist_type; } plist_type;
typedef struct { typedef struct {
plist_t node;
} PListNode; } PListNode;
%extend PListNode { // Attach these functions to struct Vector %extend PListNode { // Attach these functions to struct Vector
...@@ -35,12 +44,16 @@ typedef struct { ...@@ -35,12 +44,16 @@ typedef struct {
PListNode* node = NULL; PListNode* node = NULL;
switch (t) { switch (t) {
case PLIST_ARRAY : case PLIST_ARRAY :
node = (PListNode*) malloc(sizeof(PListNode)); node = allocate_wrapper();
node->node = plist_new_array(); if (node) {
node->node = plist_new_array();
}
break; break;
case PLIST_DICT : case PLIST_DICT :
node = (PListNode*) malloc(sizeof(PListNode)); node = allocate_wrapper();
node->node = plist_new_dict(); if (node) {
node->node = plist_new_dict();
}
break; break;
default : default :
node = NULL; node = NULL;
...@@ -50,19 +63,21 @@ typedef struct { ...@@ -50,19 +63,21 @@ typedef struct {
} }
PListNode(char* xml) { PListNode(char* xml) {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist_from_xml(xml, strlen(xml), &plist->node); plist_from_xml(xml, strlen(xml), &plist->node);
return plist; return plist;
} }
PListNode(char* bin, uint64_t len) { PListNode(char* bin, uint64_t len) {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist_from_bin(bin, len, &plist->node); plist_from_bin(bin, len, &plist->node);
return plist; return plist;
} }
~PListNode() { ~PListNode() {
plist_free($self->node); if (!$self->should_keep_plist) {
plist_free($self->node);
}
free($self); free($self);
} }
...@@ -97,20 +112,29 @@ typedef struct { ...@@ -97,20 +112,29 @@ typedef struct {
} }
PListNode* get_first_child() { PListNode* get_first_child() {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist_get_first_child(&$self->node); if (plist) {
plist->node = plist_get_first_child(&$self->node);
plist->should_keep_plist = 1;
}
return plist; return plist;
} }
PListNode* get_next_sibling() { PListNode* get_next_sibling() {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist_get_next_sibling(&$self->node); if (plist) {
plist->node = plist_get_next_sibling(&$self->node);
plist->should_keep_plist = 1;
}
return plist; return plist;
} }
PListNode* get_prev_sibling() { PListNode* get_prev_sibling() {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist_get_prev_sibling(&$self->node); if (plist) {
plist->node = plist_get_prev_sibling(&$self->node);
plist->should_keep_plist = 1;
}
return plist; return plist;
} }
...@@ -156,14 +180,20 @@ typedef struct { ...@@ -156,14 +180,20 @@ typedef struct {
} }
PListNode* find_node_by_key(char *s) { PListNode* find_node_by_key(char *s) {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist = plist_find_node_by_key($self->node, s); if (plist) {
plist->node = plist_find_node_by_key($self->node, s);
plist->should_keep_plist = 1;
}
return plist; return plist;
} }
PListNode* find_node_by_string(char* s) { PListNode* find_node_by_string(char* s) {
PListNode* plist = (PListNode*) malloc(sizeof(PListNode)); PListNode* plist = allocate_wrapper();
plist = plist_find_node_by_string($self->node, s); if (plist) {
plist->node = plist_find_node_by_string($self->node, s);
plist->should_keep_plist = 1;
}
return plist; return plist;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment