Fixed memory leak in callback message handling code

This commit is contained in:
Joseph Henry
2019-02-21 14:06:29 -08:00
parent 19be36611d
commit 884425fc5b
3 changed files with 36 additions and 22 deletions

View File

@@ -499,7 +499,8 @@ struct zts_callback_msg
netif(NULL), netif(NULL),
route(NULL), route(NULL),
path(NULL), path(NULL),
peer(NULL) {} peer(NULL),
addr(NULL) {}
/** /**
* Event identifier * Event identifier

View File

@@ -150,6 +150,9 @@ void postEvent(int eventCode) {
void freeEvent(struct zts_callback_msg *msg) void freeEvent(struct zts_callback_msg *msg)
{ {
if (!msg) {
return;
}
if (msg->node) { delete msg->node; } if (msg->node) { delete msg->node; }
if (msg->network) { delete msg->network; } if (msg->network) { delete msg->network; }
if (msg->netif) { delete msg->netif; } if (msg->netif) { delete msg->netif; }
@@ -177,10 +180,12 @@ void _process_callback_event_helper(struct zts_callback_msg *msg)
arg = msg->peer->address; arg = msg->peer->address;
} }
env->CallVoidMethod(objRef, _userCallbackMethodRef, arg, msg->eventCode); env->CallVoidMethod(objRef, _userCallbackMethodRef, arg, msg->eventCode);
freeEvent(msg);
} }
#else #else
if (_userEventCallbackFunc) { if (_userEventCallbackFunc) {
_userEventCallbackFunc(msg); _userEventCallbackFunc(msg);
freeEvent(msg);
} }
#endif #endif
} }

View File

@@ -162,9 +162,9 @@ public:
Phy<OneServiceImpl *> _phy; Phy<OneServiceImpl *> _phy;
Node *_node; Node *_node;
bool _updateAutoApply; bool _updateAutoApply;
unsigned int _multipathMode; unsigned int _multipathMode = 0;
unsigned int _primaryPort; unsigned int _primaryPort;
unsigned int _secondaryPort; unsigned int _secondaryPort = 0;
unsigned int _tertiaryPort; unsigned int _tertiaryPort;
volatile unsigned int _udpPortPickerCounter; volatile unsigned int _udpPortPickerCounter;
@@ -834,6 +834,13 @@ public:
} }
} }
inline struct zts_network_details *prepare_network_details_msg(uint64_t nwid)
{
struct zts_network_details *nd = new zts_network_details;
nd->nwid = nwid;
return nd;
}
inline void generateEventMsgs() inline void generateEventMsgs()
{ {
// Force the ordering of callback messages, these messages are // Force the ordering of callback messages, these messages are
@@ -850,33 +857,28 @@ public:
if (n->second.tap->_networkStatus == mostRecentStatus) { if (n->second.tap->_networkStatus == mostRecentStatus) {
continue; // No state change continue; // No state change
} }
struct zts_network_details *nd = new zts_network_details;
//memcpy(nd, &(pl->peers[i]), sizeof(struct zts_network_details));
nd->nwid = nwid;
switch (mostRecentStatus) { switch (mostRecentStatus) {
case ZT_NETWORK_STATUS_NOT_FOUND: case ZT_NETWORK_STATUS_NOT_FOUND:
postEvent(ZTS_EVENT_NETWORK_NOT_FOUND, (void*)nd); postEvent(ZTS_EVENT_NETWORK_NOT_FOUND, (void*)prepare_network_details_msg(nwid));
break; break;
case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: case ZT_NETWORK_STATUS_CLIENT_TOO_OLD:
postEvent(ZTS_EVENT_NETWORK_CLIENT_TOO_OLD, (void*)nd); postEvent(ZTS_EVENT_NETWORK_CLIENT_TOO_OLD, (void*)prepare_network_details_msg(nwid));
break; break;
case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION:
postEvent(ZTS_EVENT_NETWORK_REQUESTING_CONFIG, (void*)nd); postEvent(ZTS_EVENT_NETWORK_REQUESTING_CONFIG, (void*)prepare_network_details_msg(nwid));
break; break;
case ZT_NETWORK_STATUS_OK: case ZT_NETWORK_STATUS_OK:
if (tap->hasIpv4Addr() && lwip_is_netif_up(tap->netif)) { if (tap->hasIpv4Addr() && lwip_is_netif_up(tap->netif)) {
postEvent(ZTS_EVENT_NETWORK_READY_IP4, (void*)nd); postEvent(ZTS_EVENT_NETWORK_READY_IP4, (void*)prepare_network_details_msg(nwid));
} }
if (tap->hasIpv6Addr() && lwip_is_netif_up(tap->netif)) { if (tap->hasIpv6Addr() && lwip_is_netif_up(tap->netif)) {
postEvent(ZTS_EVENT_NETWORK_READY_IP6, (void*)nd); postEvent(ZTS_EVENT_NETWORK_READY_IP6, (void*)prepare_network_details_msg(nwid));
} }
// In addition to the READY messages, send one OK message // In addition to the READY messages, send one OK message
postEvent(ZTS_EVENT_NETWORK_OK, (void*)nd); postEvent(ZTS_EVENT_NETWORK_OK, (void*)prepare_network_details_msg(nwid));
break; break;
case ZT_NETWORK_STATUS_ACCESS_DENIED: case ZT_NETWORK_STATUS_ACCESS_DENIED:
postEvent(ZTS_EVENT_NETWORK_ACCESS_DENIED, (void*)nd); postEvent(ZTS_EVENT_NETWORK_ACCESS_DENIED, (void*)prepare_network_details_msg(nwid));
break; break;
default: default:
break; break;
@@ -886,26 +888,32 @@ public:
// TODO: Add ZTS_EVENT_PEER_NEW // TODO: Add ZTS_EVENT_PEER_NEW
ZT_PeerList *pl = _node->peers(); ZT_PeerList *pl = _node->peers();
struct zts_peer_details *pd;
if (pl) { if (pl) {
for(unsigned long i=0;i<pl->peerCount;++i) { for(unsigned long i=0;i<pl->peerCount;++i) {
struct zts_peer_details *pd = new zts_peer_details;
memcpy(pd, &(pl->peers[i]), sizeof(struct zts_peer_details));
// pl->peers[i].address, 0, 0, 0, NULL, 0);
if (!peerCache.count(pl->peers[i].address)) { if (!peerCache.count(pl->peers[i].address)) {
// New peer, add status
if (pl->peers[i].pathCount > 0) { if (pl->peers[i].pathCount > 0) {
pd = new zts_peer_details;
memcpy(pd, &(pl->peers[i]), sizeof(struct zts_peer_details));
postEvent(ZTS_EVENT_PEER_P2P, (void*)pd); postEvent(ZTS_EVENT_PEER_P2P, (void*)pd);
} }
if (pl->peers[i].pathCount == 0) { if (pl->peers[i].pathCount == 0) {
pd = new zts_peer_details;
memcpy(pd, &(pl->peers[i]), sizeof(struct zts_peer_details));
postEvent(ZTS_EVENT_PEER_RELAY, (void*)pd); postEvent(ZTS_EVENT_PEER_RELAY, (void*)pd);
} }
} else { }
// Previously known peer, update status
else {
if (peerCache[pl->peers[i].address] == 0 && pl->peers[i].pathCount > 0) { if (peerCache[pl->peers[i].address] == 0 && pl->peers[i].pathCount > 0) {
pd = new zts_peer_details;
memcpy(pd, &(pl->peers[i]), sizeof(struct zts_peer_details));
postEvent(ZTS_EVENT_PEER_P2P, (void*)pd); postEvent(ZTS_EVENT_PEER_P2P, (void*)pd);
} }
if (peerCache[pl->peers[i].address] > 0 && pl->peers[i].pathCount == 0) { if (peerCache[pl->peers[i].address] > 0 && pl->peers[i].pathCount == 0) {
pd = new zts_peer_details;
memcpy(pd, &(pl->peers[i]), sizeof(struct zts_peer_details));
postEvent(ZTS_EVENT_PEER_RELAY, (void*)pd); postEvent(ZTS_EVENT_PEER_RELAY, (void*)pd);
} }
} }