Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:34:53+00:00 2026-06-11T12:34:53+00:00

Please note – these builds are for VS2008/VS2010 builds I cannot use any 11

  • 0

Please note – these builds are for VS2008/VS2010 builds I cannot use any 11 constructs.

Imagine I have subscribers listening to some publisher. My publisher has a container of subscriber pointers. In my void detach(ISubscriber *), instead of locking the subscriber list, I will “NULL” out the pointer, for lack of a better word, for that subscriber.

//My container in the publisher.  Inserts to not invalidate, removals only invalidate iterators pointing to the removed element, for this reason we NULL
Container<ISubscriber *> myContainer;

Now in the publisher...
void NotifySubscribers(){
   foreach(subscriber in container){
      if(subscriber)//This is my problem
         subscriber->notify()
   }
}

Line 3 – pointer is tested and is pointing to valid object.
Before line 4 is executed, another thread NULLs the subscriber.
Line 4 – Boom.

My Question, is there a way that I can use some sort of Interlocked something such that the test and call is atomic.

e.g. for a reference counted object in the destructor, something like this works

 RefCountObject::~RefCountObject(){
    if(InterlockedDecrement(&m_count) == 0)
      delete m_data;
 }

Here, the reference counter is decremented and tested against zero automically, then and only then if equal to zero, the data is released.

Is there a way for me to do this for calling a function based on the validity of a pointer?

Edit 1: I need to clarify a little based on the comments and thank you for your replies. The publisher is not responsible for the “releasing of memory” of the Subscribers, so there will be no leak. After the notify, the publisher will go through a loop that cleans up the container by removing nulled out subscribers.

Now as for the subscribers themselves. When they detach, they are just detaching from listening to the publisher. They themselves will live on in static objects (This is the contract we are requiring). Why? Because we cannot afford to hold a lock during notification. The only other option was to use Share_Ptr, which was decided not to be incorporated into this DLL, due to versioning in the future.

I created a hand written shared_ptr, but then it occurred to me that any reference to an object that was not wrapped in a resource management class would fall into the same pitfall and just push the “requirement” that subscribers would have to make sure to not refer to any dangling references within their implementation of said subscriber.

Which brings us back to just saying, subscribers cannot be “released”, and currently all the clients that will use this are static objects. We were just looking towards the future. Some of the users are legacy apps and would not be easy to bring in enabled_shared_from_this etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:34:54+00:00Added an answer on June 11, 2026 at 12:34 pm

    is there a way that I can use some sort of Interlocked something such that the test and call is atomic.

    For the test, yes there will be a way. You just want to compare a pointer.

    To do the call, i doubt it. You will need a guard around the call, i.e. a Critical Section.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PLEASE NOTE THAT I CANNOT USE 'find' IN THE TARGET ENVIRONMENT I need to
(Please note that I have seen a similar question on StackOverflow recently, however I
Please note where I have the NSLOG. All it is displaying in the log
PLEASE NOTE: This is a CODE QUESTION NOT AN SYSTEM MANIPULATION ONE. I have
Please note I'm totally new to ASP.NET.. How can I have the username, password
Please note that I am not a java guru. I might not use the
Please note that I have already been through similar questions and their answers here
Please note I am a beginner to this. I have two questions: 1) How
please note the fiddle: http://jsfiddle.net/cBWaG/4/ I have a container with a padding. What I
I have a simple script named example : #!/bin/sh echo $'${1}' Please note that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.