TibrvMsg::markReferences()
Method
Declaration
TibrvStatus markReferences
();
Purpose
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 method 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 message, modifying 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 TimerCallback::onTimer(TibrvTimer* timer)
{
TibrvMsg* msg = (TibrvMsg*)timer->getClosure();
msg->markReferences();
TibrvMsg submsg;
msg->getMsg("foo",submsg);
...
msg->clearReferences();
msg->setSendSubject(some_subject);
/* send a message */
transport.send(*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 212: Mark and Clear References
Unless a program explicitly marks and clears references, references persist until the message is destroyed or reset.