@@ -43,7 +43,6 @@ typedef struct mod_coap_resource_obj_s {
4343    coap_resource_t *  coap_resource ;
4444    struct  mod_coap_resource_obj_s *  next ;
4545    uint8_t *  value ;
46-     unsigned char  *  uri ;
4746    uint32_t  max_age ;
4847    uint16_t  etag_value ;
4948    uint16_t  value_len ;
@@ -208,9 +207,10 @@ STATIC mod_coap_resource_obj_t* add_resource(const char* uri, uint8_t mediatype,
208207    resource -> next  =  NULL ;
209208
210209    // uri parameter pointer will be destroyed, pass a pointer to a permanent location 
211-     resource -> uri  =  m_malloc (strlen (uri ));
212-     memcpy (resource -> uri , uri , strlen (uri ));
213-     resource -> coap_resource  =  coap_resource_init ((const  unsigned char  *  )resource -> uri , strlen (uri ), 0 );
210+     unsigned char  *  uri_ptr  =  (unsigned char  * )malloc (strlen (uri ));
211+     memcpy (uri_ptr , uri , strlen (uri ));
212+     // Pass COAP_RESOURCE_FLAGS_RELEASE_URI so Coap Library will free up the memory allocated to store the URI when the Resource is deleted 
213+     resource -> coap_resource  =  coap_resource_init (uri_ptr , strlen (uri ), COAP_RESOURCE_FLAGS_RELEASE_URI );
214214    if (resource -> coap_resource  !=  NULL ) {
215215        // Add the resource to the Coap context 
216216        coap_add_resource (context -> context , resource -> coap_resource );
@@ -238,7 +238,7 @@ STATIC mod_coap_resource_obj_t* add_resource(const char* uri, uint8_t mediatype,
238238        return  resource ;
239239    }
240240    else  {
241-         m_free ( resource -> uri );
241+         free ( uri_ptr );
242242        m_del_obj (mod_coap_resource_obj_t , resource );
243243        // Resource cannot be created 
244244        return  NULL ;
@@ -278,14 +278,12 @@ STATIC void remove_resource_by_key(coap_key_t key) {
278278                    previous -> next  =  current -> next ;
279279                }
280280
281-                 // Free the URI 
282-                 m_free (current -> uri );
283281                // Free the resource in coap's scope 
284282                coap_delete_resource (context -> context , key );
285283                // Free the element in MP scope 
286-                 m_free (current -> value );
284+                 free (current -> value );
287285                // Free the resource itself 
288-                 m_free ( current );
286+                 m_del_obj ( mod_coap_resource_obj_t ,  current );
289287
290288                return ;
291289            }
@@ -320,7 +318,7 @@ STATIC void resource_update_value(mod_coap_resource_obj_t* resource, mp_obj_t ne
320318
321319    // Invalidate current data first 
322320    resource -> value_len  =  0 ;
323-     m_free (resource -> value );
321+     free (resource -> value );
324322
325323    if  (mp_obj_is_integer (new_value )) {
326324
@@ -334,7 +332,7 @@ STATIC void resource_update_value(mod_coap_resource_obj_t* resource, mp_obj_t ne
334332        }
335333
336334        // Allocate memory for the new data 
337-         resource -> value  =  m_malloc (resource -> value_len );
335+         resource -> value  =  malloc (resource -> value_len );
338336        memcpy (resource -> value , & value , sizeof (value ));
339337
340338    } else  {
@@ -344,7 +342,7 @@ STATIC void resource_update_value(mod_coap_resource_obj_t* resource, mp_obj_t ne
344342        resource -> value_len  =  value_bufinfo .len ;
345343
346344        // Allocate memory for the new data 
347-         resource -> value  =  m_malloc (resource -> value_len );
345+         resource -> value  =  malloc (resource -> value_len );
348346        memcpy (resource -> value , value_bufinfo .buf , resource -> value_len );
349347    }
350348}
@@ -748,16 +746,13 @@ STATIC coap_pdu_t * modcoap_new_request
748746// Helper function to create a new option for a request message 
749747STATIC  coap_list_t  *  modcoap_new_option_node (unsigned short  key , unsigned int   length , unsigned char   * data ) {
750748
751-     coap_list_t  * node  =  m_malloc (sizeof (coap_list_t ) +  sizeof (coap_option ) +  length );
749+     coap_list_t  * node  =  malloc (sizeof (coap_list_t ) +  sizeof (coap_option ) +  length );
752750    if  (node ) {
753751        coap_option  * option ;
754752        option  =  (coap_option  * )(node -> data );
755753        COAP_OPTION_KEY (* option ) =  key ;
756754        COAP_OPTION_LENGTH (* option ) =  length ;
757755        memcpy (COAP_OPTION_DATA (* option ), data , length );
758-     } else  {
759-         m_free (node );
760-         node  =  NULL ;
761756    }
762757
763758    return  node ;
@@ -937,7 +932,7 @@ STATIC mp_obj_t mod_coap_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
937932    // Only 1 context is supported currently 
938933    if (initialized  ==  false) {
939934
940-         MP_STATE_PORT (coap_ptr ) =  m_malloc ( sizeof ( mod_coap_obj_t ) );
935+         MP_STATE_PORT (coap_ptr ) =  m_new_obj ( mod_coap_obj_t );
941936        coap_obj_ptr  =  MP_STATE_PORT (coap_ptr );
942937        coap_obj_ptr -> context  =  NULL ;
943938        coap_obj_ptr -> resources  =  NULL ;
@@ -1235,19 +1230,21 @@ STATIC mp_obj_t mod_coap_send_request(mp_uint_t n_args, const mp_obj_t *pos_args
12351230            //TODO: allocate the proper length 
12361231            size_t  length  =  300 ;
12371232            unsigned char  *  path  =  malloc (length );
1238-             int  segments  =  coap_split_path (coap_uri .path .s , coap_uri .path .length , path , & length );
1233+             // Need to use a different pointer because when the segments are composed the pointer itself is moved 
1234+             unsigned char  *  path_segment  =  path ;
1235+             int  segments  =  coap_split_path (coap_uri .path .s , coap_uri .path .length , path_segment , & length );
12391236
12401237            // Insert the segments as separate URI-Path options 
12411238            while  (segments -- ) {
1242-                 node  =  modcoap_new_option_node (COAP_OPTION_URI_PATH , COAP_OPT_LENGTH (path ), COAP_OPT_VALUE (path ));
1239+                 node  =  modcoap_new_option_node (COAP_OPTION_URI_PATH , COAP_OPT_LENGTH (path_segment ), COAP_OPT_VALUE (path_segment ));
12431240                if (node  !=  NULL ) {
12441241                    LL_APPEND (coap_obj_ptr -> optlist , node );
12451242                }
1246- 
1247-                 path  +=  COAP_OPT_SIZE (path );
1243+                  // Move the path_segment pointer to the next segment 
1244+                 path_segment  +=  COAP_OPT_SIZE (path_segment );
12481245            }
12491246
1250- 
1247+              // Free up the memory using the pointer pointing to the beginning of the memory area 
12511248            free (path );
12521249
12531250            // Put Content Format option if given 
@@ -1271,7 +1268,7 @@ STATIC mp_obj_t mod_coap_send_request(mp_uint_t n_args, const mp_obj_t *pos_args
12711268        while (coap_obj_ptr -> optlist  !=  NULL ) {
12721269            next  =  coap_obj_ptr -> optlist -> next ;
12731270            coap_obj_ptr -> optlist -> next  =  NULL ;
1274-             m_free (coap_obj_ptr -> optlist );
1271+             free (coap_obj_ptr -> optlist );
12751272            coap_obj_ptr -> optlist  =  next ;
12761273        }
12771274
0 commit comments