* Deleting pointers in a loop

For plugin authors to discuss plugin programming
Post Reply
User avatar
Mark1834
Megastar
Posts: 2519
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Deleting pointers in a loop

Post by Mark1834 »

My normal approach for deleting multiple items in a loop is to define a table to hold copies of the pointers to be deleted. In the loop body I use table.insert(tblX, p:Clone()), followed by for _, p in ipairs(tblX) do fhDeleteItem(p) end to do the actual deletions.

A plugin bug was caused by not carrying out the second step prior to performing another action that depended on the actual deletion taking place. It is feels safer to delete as you go along with fhDeleteItem(p:Clone()) in the body of the loop, and dispense with the table, but are there any downsides to doing it this way, for example in very large projects?
Mark Draper
User avatar
Jane
Site Admin
Posts: 8518
Joined: 01 Nov 2002 15:00
Family Historian: V7
Location: Somerset, England
Contact:

Re: Deleting pointers in a loop

Post by Jane »

The store them up and do at the end is the way I work too. Mainly because if you delete in-line you can lose your point in the loop. This is especially true if what you are deleting may have a branch below it.

If you are deleting in-line you need to be sure you have read the next field, which will not be deleted by the delete.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
User avatar
Mark1834
Megastar
Posts: 2519
Joined: 27 Oct 2017 19:33
Family Historian: V7
Location: South Cheshire, UK

Re: Deleting pointers in a loop

Post by Mark1834 »

Thanks Jane, that makes sense. The safest technique may be to continue with the table, but ensure that the pointers are deleted and the table emptied as soon as the looped steps are completed rather than wait until the end of the function.
Mark Draper
Post Reply