* pointer goes null

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
Ron Melby
Megastar
Posts: 928
Joined: 15 Nov 2016 15:40
Family Historian: V6.2

pointer goes null

Post by Ron Melby »

I just cannot see the error that occurs here,
after execution of --1-- fptr contains a valid FAMC link.
after execution of --2-- fc contains 1 which is correct for the iptr record.
HOWEVER: fptr is now (null) ???? I have included rtvElemCount which should not change fptr (near as I am able to see) and fptr is local and there is no other global fptr out there.

is it so simple I am just not seeing it?


function rtvFAMPtr(iptr)
local fptr = fhNewItemPtr() -- ALL FAMC
local cptr = fhNewItemPtr() -- space pointer
local aptr = fhNewItemPtr() -- ADOP
local ftbl = {}

--1-- fptr = fhGetItemPtr(iptr, '~.FAMC')
--2-- local fc = rtvElemCount(fptr)
if fc > 0 then
while fptr:IsNotNull() do
local pedi = fhGetItemText(fptr, '~.PEDI')
if pedi == ''
or pedi == 'Birth'
or pedi == 'Illegitimate' then
table.insert(ftbl, fptr:Clone())
end -- ifpedi
fptr:MoveNext('SAME_TAG')
end -- famptr notnull

cptr = fhGetItemPtr(iptr, ' ~.ADOP')
local ac = rtvElemCount(cptr)
if ac > 0 then
while cptr:IsNotNull() do
aptr = fhGetItemPtr(cptr, '~.FAMC')
if aptr:IsNotNull() then
local cadop = fhGetValueAsLink(aptr)
for i, v in pairs(ftbl) do
local cfam = fhGetValueAsLink(v)
if cadop:IsSame(cfam) then
table.remove(ftbl, i)
end
end
end
cptr:MoveNext('SAME_TAG')
end
end
end
assert(#ftbl == 1, 'Failure in rcdid: ' .. fhGetRecordId(iptr))
return ftbl[1]
end -- fn rtvFAMptr

function rtvElemCount(eptr)
-- Count instances of eptr TAG
local elem = 0

while eptr:IsNotNull() do
elem = elem + 1
eptr:MoveNext('SAME_TAG')
end
return elem
end -- fn rtvElemCount
FH V.6.2.7 Win 10 64 bit
User avatar
tatewise
Megastar
Posts: 28414
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: pointer goes null

Post by tatewise »

rtvElemCount does change fptr to null because it is passed by reference to eptr which is reduced to null.
i.e. fptr and eptr are the same pointer!

Use local fc = rtvElemCount(fptr:Clone()) and it will work OK.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Post Reply