Start a new topic

Init & CloseDown

General RE API question

If I am doing something like

        Dim oRec As New CRecord
        oRec.Init(Import.SessionContext)
        Dim iAck As CTributeAcknowledgee
        Dim sReturn As String = ""
        Dim iCount As Integer = 0


        For Each iAck In oTribute.Acknowledgees
            oRec.LoadByField(ERecordsFields.RECORDS_fld_IMPORT_ID, iAck.Fields(ETributeAcknowledgeeFields.TributeAcknowledgee_fld_IMPORT_ID))
            iCount = iCount + 1
        Next iAck

        oRec.CloseDown()
        oRec = Nothing


Do I need to .CloseDown() each oRec in the loop or is it ok to just do it once at the end?


You should do something like this:

Dim oRec As CRecord
Dim iAck As CTributeAcknowledgee
Dim sReturn As String = ""
Dim iCount As Integer = 0

For Each iAck In oTribute.Acknowledgees
oRec = new CRecord
oRec.Init(Import.SessionContext)
oRec.LoadByField(ERecordsFields.RECORDS_fld_IMPORT_ID, iAck.Fields(ETributeAcknowledgeeFields.TributeAcknowledgee_fld_IMPORT_ID))
iCount = iCount + 1
oRec.CloseDown()
oRec = Nothing
Next iAck

Closing down the Blackbaud objects is important. Forgetting to can cause out of memory issues.

A tricky example we came across in the past was with the Blackbaud collection objects. Take the below code for example.

oRec.Actions.Item(1).Fields(blackbaud.PIA.RE7.BBREAPI.EACTIONFields.ACTION_fld_DESCRIPTION) = "New Desc"
oRec.Actions.Item(1).Save

"Item" is actually a function that returns an initialized CAction. The proper way to do the same bit of code is below

dim act as CAction = oRec.Actions.Item(1)
act..Fields(blackbaud.PIA.RE7.BBREAPI.EACTIONFields.ACTION_fld_DESCRIPTION) = "New Desc"
act.Save()
act.CloseDown()
act = Nothing

Awesome, thanks Nic. I'll figure this out eventually :)
actually, I have a question about that last bit.

If I am looping through actions with a For Each does that mean I need to do something to clean up that Action object?

ex. in AfterConstituentOpen so I already have oRec but I want to look through the actions

For Each oAction As BBREAPI.CAction In oRec.Actions
blah
Next
Yes you do need to CloseDown in that scenario. Generally if an object has a CloseDown method then you should use it. The only reason to not use it is if the object was passed to you through the IOM API. CloseDown on oRec/oInd/oOrg/etc. will cause issues within IOM.
ah, well, this might be the reason that I see RE grow in memory after running imports. I guess I'll have to go through my code and clean all of that up.
Login or Signup to post a comment