<p style="white-space: pre-wrap; word-wrap: break-word;">You are not carefully reading the clarification of the changes I have made. Please read the above explanations again.</p><p style="white-space: pre-wrap; word-wrap: break-word;">Note that I did not come up with this approach to managing objects, I only adhere to this rule.</p><p style="white-space: pre-wrap; word-wrap: break-word;">The subscription logic allows you to subscribe not only to one but also to several applications to the same subscription. The only difference is that there are two ways to store objects for later search and retrieval:<br>1. Vector for technology subscriptions (tech_subscriptions)<br>2. Special container for resource subscriptions (endpoint_subscriptions)</p><p style="white-space: pre-wrap; word-wrap: break-word;">By analogy with COM, the reference count of an object must be equal to the number of references to that object.<br>If the object's reference count becomes 0, then the object is self-destructing.<br>If a function uses RAII_VAR for an object being processed, then the getter or create function must take care of incrementing the object's reference count by 1 when it returns.<br>Note that calling ao2_link increments the object reference count by 1, while ao2_unlink decrements the object reference count by 1.</p><p style="white-space: pre-wrap; word-wrap: break-word;">Once again, let's briefly consider the situation when we have one subscription, and several applications that "want" to subscribe to this subscription:</p><p style="white-space: pre-wrap; word-wrap: break-word;">App subscription 1:<br>1. Search for a subscription in the repository. No subscription found. Create new subscription object (link count = 2)<br>2. Checking whether the application has a current subscription. No subscription found<br>3. Adding information about the application to the subscription object (sub-> applications)<br>4. Checking the number of signed applications for a subscription. 1 application signed. Since the link counter of the subscription object will be equal to 1 after exiting the messaging_app_subscribe_endpoint function, there is no need to increase the link counter of the subscription object.<br>5. Exit the function. RAII_VAR fires. Subscription Object Link Count = 1.<br>As a result, we have: the number of applications = 1, the counter of reference to the subscription object = 1. That is. 1 application refers to the subscription.</p><p style="white-space: pre-wrap; word-wrap: break-word;">App 2 subscription:<br>1. Search for a subscription in the repository. Subscription found. Retrieving a subscription object (reference count = 2)<br>2. Checking whether the application has a current subscription. No subscription found<br>3. Adding information about the application to the subscription object (sub-> applications)<br>4. Checking the number of signed applications for a subscription. Signed 2 applications. Since the link counter of the subscription object, after exiting the messaging_app_subscribe_endpoint function, will be equal to 1, and the subscribed applications will be 2, then it is necessary to increase the link counter of the subscription object by 1 (link counter = 3).<br>5. Exit the function. RAII_VAR fires. Subscription Object Link Count = 2.<br>As a result, we have, after exiting the function: the number of applications = 2, the counter of reference to the subscription object = 2. That is, the subscription is referenced by 2 applications.</p><p style="white-space: pre-wrap; word-wrap: break-word;">App 3 subscription:<br>1. Search for a subscription in the repository. Subscription found. Retrieving a subscription object (reference count = 3)<br>2. Checking whether the application has a current subscription. No subscription found<br>3. Adding information about the application to the subscription object (sub-> applications)<br>4. Checking the number of signed applications for a subscription. 3 applications signed. Since the link counter of the subscription object, after exiting the messaging_app_subscribe_endpoint function, will be equal to 2, and the subscribed applications will be 3, then it is necessary to increase the link counter of the subscription object by 1 (link counter = 4).<br>5. Exit the function. RAII_VAR fires. Subscription Object Link Count = 3.<br>As a result, we have, after exiting the function: the number of applications = 3, the counter of reference to the subscription object = 3. That is, the subscription is referenced by 3 applications.</p><p style="white-space: pre-wrap; word-wrap: break-word;"><br>Now let's briefly consider the situation when we have one subscription, and several applications that "want" to unsubscribe from this subscription (they were previously subscribed to it):</p><p style="white-space: pre-wrap; word-wrap: break-word;">Unsubscribe application 1:<br>1. Search for a subscription in the repository. Subscription found. Retrieving a subscription object (reference count = 4)<br>2. Checking whether the application has a current subscription. Subscription found<br>3. Removing information about the application from the subscription object (sub-> applications)<br>4. Checking the number of signed applications for a subscription. Signed 2 applications.<br>5. Decrease the link count of the subscription object by 1 (link count = 3)<br>6. Exit the function. RAII_VAR fires. Subscription Object Link Count = 2.<br>As a result, we have: the number of applications = 2, the counter of reference to the subscription object = 2. That is. the subscription is referenced by 2 applications.</p><p style="white-space: pre-wrap; word-wrap: break-word;">Unsubscribe application 2:<br>1. Search for a subscription in the repository. Subscription found. Retrieving a subscription object (reference count = 3)<br>2. Checking whether the application has a current subscription. Subscription found<br>3. Removing information about the application from the subscription object (sub-> applications)<br>4. Checking the number of signed applications for a subscription. 1 application signed.<br>5. Decrease the link count of the subscription object by 1 (link count = 2)<br>6. Exit the function. RAII_VAR fires. Subscription Object Link Count = 1.<br>As a result, we have: the number of applications = 1, the counter of reference to the subscription object = 1. That is. 1 application refers to the subscription.</p><p style="white-space: pre-wrap; word-wrap: break-word;">Unsubscribe application 3:<br>1. Search for a subscription in the repository. Subscription found. Retrieving a subscription object (reference count = 2)<br>2. Checking whether the application has a current subscription. Subscription found<br>3. Removing information about the application from the subscription object (sub-> applications)<br>4. Checking the number of signed applications for a subscription. 0 applications signed. Removing a pointer to a subscription object from storage.<br>4.1. If the subscription refers to technology subscriptions (tech_subscriptions), then only the pointer is removed from the list of those.<br>4.2. If the subscription is for resource subscriptions (endpoint_subscriptions), then ao2_unlink is called, which decrements the link count of the subscription object by 1 (ie link count = 1). If you do not increase the counter of reference of the Subscription object back by 1, then the object will already be deleted in paragraph 5, and an exception will be raised in paragraph 6. Therefore, we increase the subscription link count by 1 back (i.e. link count = 2).<br>5. Decrease the link count of the subscription object by 1 (link count = 1)<br>6. Exit the function. RAII_VAR fires. Subscription object reference count = 0. Subscription object self-destructs<br>As a result, we have: the number of applications = 0, the counter of reference to the subscription object = 0. That is. there are no references to the subscription and the subscription object has been destroyed.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15762">View Change</a></p><ul style="list-style: none; padding: 0;"></ul><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15762">change 15762</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/15762"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ia91b15f8e5ea68f850c66889a6325d9575901729 </div>
<div style="display:none"> Gerrit-Change-Number: 15762 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: Evgenios Muratidis <jone1984@hotmail.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Comment-Date: Mon, 24 May 2021 12:42:58 +0000 </div>
<div style="display:none"> Gerrit-HasComments: No </div>
<div style="display:none"> Gerrit-Has-Labels: No </div>
<div style="display:none"> Gerrit-MessageType: comment </div>