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

  • SEARCH
  • Home
  • 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 173553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:21:51+00:00 2026-05-11T13:21:51+00:00

I just got burned by a bug that is partially due to my lack

  • 0

I just got burned by a bug that is partially due to my lack of understanding, and partially due to what I think is suboptimal design in our codebase. I’m curious as to how my 5-minute solution can be improved.

We’re using ref-counted objects, where we have AddRef() and Release() on objects of these classes. One particular object is derived from the ref-count object, but a common function to get an instance of these objects (GetExisting) hides an AddRef() within itself without advertising that it is doing so. This necessitates doing a Release at the end of the functional block to free the hidden ref, but a developer who didn’t inspect the implementation of GetExisting() wouldn’t know that, and someone who forgets to add a Release at the end of the function (say, during a mad dash of bug-fixing crunch time) leaks objects. This, of course, was my burn.

 void SomeFunction(ProgramStateInfo *P) {    ThreadClass *thread = ThreadClass::GetExisting( P );    // some code goes here    bool result = UseThreadSomehow(thread);    // some code goes here    thread->Release();  // Need to do this because GetExisting() calls AddRef() } 

So I wrote up a little class to avoid the need for the Release() at the end of these functions.

 class ThreadContainer { private:     ThreadClass *m_T; public:     ThreadContainer(Thread *T){ m_T = T; }     ~ThreadContainer() { if(m_T) m_T->Release(); }     ThreadClass * Thread() const { return m_T; } }; 

So that now I can just do this:

 void SomeFunction(ProgramStateInfo *P) {    ThreadContainer ThreadC(ThreadClass::GetExisting( P ));    // some code goes here    bool result = UseThreadSomehow(ThreadC.Thread());    // some code goes here    // Automagic Release() in ThreadC Destructor!!! } 

What I don’t like is that to access the thread pointer, I have to call a member function of ThreadContainer, Thread(). Is there some clever way that I can clean that up so that it’s syntactically prettier, or would anything like that obscure the meaning of the container and introduce new problems for developers unfamiliar with the code?

Thanks.

  • 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. 2026-05-11T13:21:51+00:00Added an answer on May 11, 2026 at 1:21 pm

    use boost::shared_ptr it is possible to define your own destructor function, such us in next example: http://www.boost.org/doc/libs/1_38_0/libs/smart_ptr/sp_techniques.html#com

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

Sidebar

Related Questions

i just got a call from Hosting company, they claimed that my application creates
We just got our first major Perl CGI assignment in my CS class. Our
I just got burned by a widget. I could see the cause of the
Just got curious :) I have a Map object map , and I can
I just got an old pentium 4 and I thought, that it would be
just got a couple of new PanelPC's running Windows Embedded 2009 that we are
I just got burned because I used find_index on an array on my dev
I just got ZyXEL NWD2105, a small wlan usb adapter that i found on
We just got new servers for our new system and I want to know
I just got Delphi 2009 and have previously read some articles about modifications 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.