tibrvMsg_MarkReferences()
Function
Declaration
tibrv_statustibrvMsg_MarkReferences
(
tibrvMsg message);
Purpose
Mark references in this message.
Parameter |
Description |
|
Mark references in this message. |
Remarks
Extracting pointer data from a message field creates a snapshot of that data. The snapshot remains associated with the message until the program destroys the message. However, in rare situations snapshots can accumulate within a program, causing unbounded memory growth. This function gives programs explicit control over snapshot references; by clearing references, the program declares that it no longer needs the references that arise as side effects of calls that get a message field.
For example, consider a program fragment that repeatedly sends a template message, getting and updating fields within a nested submessage before each send call. Each call to extract the nested message produces a snapshot reference. By surrounding the get operation with a mark and clear pair (with the clear call occurring at any time after the get call), the program releases the reference, which helps control memory usage.
void myTimerCB(
tibrvEvent myTimer,
tibrvMsg empty,
void* closure_msg_arg)
{
tibrvMsg msg = (tibrvMsg)closure_msg_arg;
tibrvMsg submsg;
tibrvMsg_MarkReferences(msg);
tibrvMsg_GetMsg(msg, "foo", &submsg);
...
tibrvMsg_ClearReferences(msg);
tibrvMsg_SetSendSubject(msg, some_subject);
tibrvTransport_Send(myTransport, msg);
}
Every call to tibrvMsg_MarkReferences() must be paired with a call to tibrvMsg_ClearReferences(). It is legal to mark references several times, as long as the program eventually clears all the marks. To understand this idea, it is helpful to think of get and mark as pushdown operations, and clear as a pop operation. Mark and Clear References illustrates that each clear call deletes snapshots back to the most recent mark.
Figure 172: Mark and Clear References
Unless a program explicitly marks and clears references, references persist until the message is destroyed or reset.