@@ -83,6 +83,32 @@ keepAliveCb(evutil_socket_t fd, short flags, void * arg)
8383    // do nothing... 
8484}
8585
86+ static  void 
87+ natsLibeventEvents_free (natsLibeventEvents  * nle , bool  processDetachedEvents )
88+ {
89+     if  (nle -> read  !=  NULL )
90+         event_free (nle -> read );
91+     if  (nle -> write  !=  NULL )
92+         event_free (nle -> write );
93+     if  (nle -> keepActive  !=  NULL )
94+     {
95+         event_active (nle -> keepActive , 0 , 0 );
96+         event_free (nle -> keepActive );
97+     }
98+     if  (processDetachedEvents )
99+         natsConnection_ProcessDetachedEvent (nle -> nc );
100+     free (nle );
101+ }
102+ 
103+ // This callback is invoked from the event loop thread and will free the 
104+ // `natsLibeventEvents` object. 
105+ static  void 
106+ _freeCb (evutil_socket_t  ignoredSocket , short  ignoredEvent , void  * arg )
107+ {
108+     natsLibeventEvents  * nle  =  (natsLibeventEvents * ) arg ;
109+     natsLibeventEvents_free (nle , true);
110+ }
111+ 
86112/** \brief Attach a connection to the given event loop. 
87113 * 
88114 * This callback is invoked after `NATS` library has connected, or reconnected. 
@@ -101,6 +127,7 @@ natsLibevent_Attach(void **userData, void *loop, natsConnection *nc, natsSock so
101127    struct  event_base    * libeventLoop  =  (struct  event_base * ) loop ;
102128    natsLibeventEvents   * nle           =  (natsLibeventEvents * ) (* userData );
103129    natsStatus           s              =  NATS_OK ;
130+     bool                 created        =  false;
104131
105132    // This is the first attach (when reconnecting, nle will be non-NULL). 
106133    if  (nle  ==  NULL )
@@ -109,6 +136,8 @@ natsLibevent_Attach(void **userData, void *loop, natsConnection *nc, natsSock so
109136        if  (nle  ==  NULL )
110137            return  NATS_NO_MEMORY ;
111138
139+         // Indicate that we have created the object here (in case we get a failure). 
140+         created    =  true;
112141        nle -> nc    =  nc ;
113142        nle -> loop  =  libeventLoop ;
114143
@@ -153,8 +182,8 @@ natsLibevent_Attach(void **userData, void *loop, natsConnection *nc, natsSock so
153182
154183    if  (s  ==  NATS_OK )
155184        * userData  =  (void * ) nle ;
156-     else 
157-         natsLibevent_Detach (( void * )  nle );
185+     else   if  ( created ) 
186+         natsLibeventEvents_free ( nle , false );
158187
159188    return  s ;
160189}
@@ -224,27 +253,6 @@ natsLibevent_Write(void *userData, bool add)
224253    return  (res  ==  0  ? NATS_OK  : NATS_ERR );
225254}
226255
227- // This callback is invoked from the event loop thread and will free the 
228- // `natsLibeventEvents` object. 
229- static  void 
230- _freeCb (evutil_socket_t  ignoredSocket , short  ignoredEvent , void  * arg )
231- {
232-     natsLibeventEvents   * nle  =  (natsLibeventEvents * ) arg ;
233- 
234-     if  (nle -> read  !=  NULL )
235-         event_free (nle -> read );
236-     if  (nle -> write  !=  NULL )
237-         event_free (nle -> write );
238-     if  (nle -> keepActive  !=  NULL )
239-     {
240-         event_active (nle -> keepActive , 0 , 0 );
241-         event_free (nle -> keepActive );
242-     }
243-     // This will release the connection that is retained by the library on the first attach. 
244-     natsConnection_Destroy (nle -> nc );
245-     free (nle );
246- }
247- 
248256/** \brief The connection is closed, it can be safely detached. 
249257 * 
250258 * When a connection is closed (not disconnected, pending a reconnect), this 
0 commit comments