Skip to content

list: about add macro LIST_FOREACH_SAFE #1199

Description

@jobo-zt

sreimers, Thank you for your reply.

  1. The first approach directly modifies the le pointer during the list traversal. If an exception occurs or the list structure is modified in list_unlink or mem_deref, it may cause the traversal to be interrupted or access already freed memory.
  2. The second approach uses the LIST_FOREACH_SAFE macro, which safely traverses the list using two pointers, le and n. Even if the list structure is modified in list_unlink or mem_deref, it will not affect the safety of the traversal.

I tried to use it, but it didn't work.

#if 0	
	LIST_FOREACH_SAFE(&turndp()->rm_map, le, n)
	{
		struct udp_socks *uks = list_ledata(le);
		if (thrd_id == uks->thrd_id) {

			udp_thread_detach(uks->rel_us);
			udp_thread_detach(uks->rsv_us);

			list_unlink(&uks->le);
			mem_deref(uks);
		}
	}
#endif
#if 1
	le = list_head(&turndp()->rm_map);
	while (le)
	{
		struct udp_socks *uks = list_ledata(le);
		if (thrd_id == uks->thrd_id) {
			le = le->next;

			udp_thread_detach(uks->rel_us);
			udp_thread_detach(uks->rsv_us);
			
			list_unlink(&uks->le);
			mem_deref(uks);
		}
	}
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions