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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:39:40+00:00 2026-05-11T08:39:40+00:00

So I have this library code, see… class Thing { public: class Obj {

  • 0

So I have this library code, see…

class Thing { public:      class Obj      {      public:         static const int len = 16;          explicit Obj(char *str)         {             strncpy(str_, str, len);         }          virtual void operator()() = 0;      private:         char str_[len];     };      explicit Thing(vector<Obj*> &objs) : objs_(objs) {}      ~Thing() {         for(vector<Obj*>::iterator i = objs_.begin(); i != objs_.end(); ++i) {             delete *i;         }     }  private:     vector<Obj*> objs_; } 

And in my client code…

   class FooObj : public Thing::Obj     {         virtual void operator()() {             //do stuff         }     }      class BarObj : public Thing::Obj     {         virtual void operator()() {             //do different stuff         }     }  vector<Objs*> objs; int nStructs = system_call(*structs); for(int i = 0; i < nStructs; i++) {     objs.push_back(newFooObj(structs[i].str)); } objs.push_back(newBarObj('bar1'); objs.push_back(newBarObj('bar2');  Thing thing(objs); // thing does stuff, including call Obj::()() on the elements of the objs_ vector 

The thing I’m stuck on is exception safety. As it stands, if any of the Obj constructors throw, or the Thing constructor throws, the Objs already in the vector will leak. The vector needs to contain pointers to Objs because they’re being used polymorphically. And, I need to handle any exceptions here, because this is being invoked from an older codebase that is exception-unaware.

As I see it, my options are:

  1. Wrap the client code in a giant try block, and clean up the vector in the catch block.
  2. Put try blocks around all of the allocations, the catch blocks of which call a common cleanup function.
  3. Some clever RAII-based idea that I haven’t thought of yet.
  4. Punt. Realistically, if the constructors throw, the application is about to go down in flames anyway, but I’d like to handle this more gracefully.
  • 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-11T08:39:41+00:00Added an answer on May 11, 2026 at 8:39 am

    Since your Thing destructor already knows how to clean up the vector, you’re most of the way towards a RAII solution. Instead of creating the vector of Objs, and then passing it to Thing’s constructor, you could initialize Thing with an empty vector and add a member function to add new Objs, by pointer, to the vector.

    This way, if an Obj’s constructor throws, the compiler will automatically invoke Thing’s destructor, properly destroying any Objs that were already allocated.

    Thing’s constructor becomes a no-op:

    explicit Thing() {} 

    Add a push_back member:

    void push_back(Obj *new_obj) { objs_.push_back(new_obj); } 

    Then the allocation code in your client becomes:

    Thing thing(objs); int nStructs = system_call(*structs); for(int i = 0; i < nStructs; i++) {     thing.push_back(newFooObj(structs[i].str)); } thing.push_back(newBarObj('bar1'); thing.push_back(newBarObj('bar2'); 

    As another poster suggested, a smart pointer type inside your vector would also work well. Just don’t use STL’s auto_ptr; it doesn’t follow normal copy semantics and is therefore unsuitable for use in STL containers. The shared_ptr provided by Boost and the forthcoming C++0x would be fine.

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

Sidebar

Ask A Question

Stats

  • Questions 192k
  • Answers 192k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do it in an intranet environment by altering… May 12, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer KVM has much better isolation than OpenVZ and in my… May 12, 2026 at 6:25 pm
  • Editorial Team
    Editorial Team added an answer Have you tried swapping your quotes? jQuery might be using… May 12, 2026 at 6:25 pm

Related Questions

So I have this library code, see... class Thing { public: class Obj {
I have been trying very hard to achieve rounded corners with IE6+jquery ui tabs.
Before I begin: I have spent a long time on many forums (including Stack
I recently needed to serialize a datatable to JSON. Where I'm at we're still
Even though the solution is so obvious I should have never have posted this,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.