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 8963387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:20:37+00:00 2026-06-15T16:20:37+00:00

Consider the case where a function foo(MyClass* mc) is supposed to retain a copy

  • 0

Consider the case where a function foo(MyClass* mc) is supposed to retain a copy of mc into an internal data structure, and guarantee that the object will be deleted when no longer used.

void foo(MyClass* mc) // acquires ownership of mc; may throw
{
  // code that may throw
  bar(mc); // acquires mc; may also throw
}

The problem arises when this function executes code that might throw (for instance, an OutOfMemory exception). If the exception is raised before the pointer was saved to the data structure, the object should obviously be freed before the function unwinds since the caller is no more responsible for it (the caller doesn’t even know if the pointer was actually saved in the data structure or not).

One could use RAII to handle this with scope guards, but it seems very clumsy, and produces some overhead (it has to be done in every single function that acquires a pointer).

Would one really need to do this every time a dynamically-allocated object is acquired, or is there a neater way to do it?!

template <class T>
struct VerifyAcq {
  T* ptr;
  bool done;
  VerifyAcq(T* ptr):ptr(ptr) { done = false; }
  ~VerifyAcq() {
    if (!done) delete ptr;
  }
};

void foo(MyClass* mc) // acquires mc; may throw
{
  VerifyAcq<MyClass> va(mc);
  // code that may throw
  bar(mc); // acquires mc; may throw; must implement the same mechanism!
  va.done = true;
}
// Note: there might be no public way of "undoing" what bar has done (no rollbak)
// and even if there was, what if it could also throw?...

The exception cannot be caught by the caller in order to delete the pointer, because before throwing an exception, the function may have successfully added the pointer to the data structure, and freeing the object would make the data structure unsound (dangling pointer).

  • 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-15T16:20:38+00:00Added an answer on June 15, 2026 at 4:20 pm

    When I started reading the code, I stopped at this point:

    void foo(MyClass* mc) // acquires ownership of mc; may throw
    

    A comment is not the best way to document ownership acquisition. The best way to do that is to use the type system.

     void foo(std::unique_ptr<MyClass> mc) // no comment required
    

    Fixing the interfaces also ends up solving the problem.

    (If you don’t have unique_ptr in your standard library, there are alternatives like for example, Howard Hinnant’s emulation in C++03).

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

Sidebar

Related Questions

I'm using LINQ to query data. Consider a case where the user only wants
Please consider the following simple use case: public class Foo { public virtual int
Consider the code below: foo = list(First List, 1, 2, 3) bar = function(x)
Consider the following case: void Set(const std::function<void(int)> &fn); void Set(const std::function<void(int, int)> &fn); Now
Please consider the simple express.js route: app.get(/test, function(req, res) { if(condition1) { res.render(foo, {
Consider the case where a certain mocked function is expected to be called several
virtual function resolution happens with pointer/reference and not with object. Now consider below example:
Consider: Private Function ViewPropertyCast(ByVal type As String) As String Select Case type Case smallint
Consider this function signature: Private Void TextBox1_TextChange(Object Sender, EventArgs e) As far as my
Consider the case where a virtual function in base class is not overriden in

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.