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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:22:39+00:00 2026-05-16T04:22:39+00:00

Consider the following class structure: class Filter { virtual void filter() = 0; virtual

  • 0

Consider the following class structure:

class Filter
{
    virtual void filter() = 0;
    virtual ~Filter() { }
};

class FilterChain : public Filter
{
    FilterChain(collection<Filter*> filters)
    {
         // copies "filters" to some internal list
         // (the pointers are copied, not the filters themselves)
    }

    ~FilterChain()
    {
         // What do I do here?
    }

    void filter()
    {
         // execute filters in sequence
    }
};

I’m exposing the class in a library, so I don’t have control over how it will be used.

I’m currently having some design issues regarding ownership of the Filter objects FilterChain is holding pointers to. More specifically, here are two possible usage scenarios for FilterChain:

  • Scenario A: some of the functions in my library are constructing a (possibly complex) filter chain, allocating memory as necessary, and returning a newly-allocated FilterChain object. For example, one of these functions constructs a filter chain from a file, which can describe arbitrarily-complex filters (including filter chains of filter chains, etc.). The user of the function is responsible for object destruction once the job is done.
  • Scenario B: the user has access to a bunch of Filter objects, and wants to combine them in filter chains in a specific manner. The user constructs FilterChain objects for its own use, then destroy them when he’s done with them. The Filter objects must not be destroyed when a FilterChain referencing them is destroyed.

Now, the two simplest ways to manage ownership in the FilterChain object are:

  • FilterChain own the Filter objects. This means the objects referenced by FilterChain are destroyed in FilterChain‘s destructor. Which is incompatible with scenario B.
  • FilterChain does not own the Filter objects. This means FilterChain‘s destructor does nothing. Now there is a problem with scenario A, because the user would have to know the internal structure of all the Filter objects involved in order to destroy them all without missing one, as the parent FilterChain does not do it itself. That’s just bad design, and asking for memory leaks.

Consequently, I need something more complicated. My first guess is to design a smart pointer with a settable boolean flag indicating whether or not the smart pointer owns the object. Then instead of taking a collection of pointers to Filter objects, FilterChain would take a collection of smart pointers to Filter objects. When FilterChain‘s destructor is called, it would destroy the smart pointers. The destructor of the smart pointer itself would then destroy the object being pointed to (a Filter object) if and only if the boolean flag indicating ownership is set.

I get the feeling that this problem is commonplace in C++, but my web searches for popular solutions or clever design patterns were not very successful. Indeed, auto_ptr doesn’t really help here and shared_ptr seems overkill. So, is my solution a good idea or not?

  • 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-05-16T04:22:40+00:00Added an answer on May 16, 2026 at 4:22 am

    Smart pointers here are not overkill: obviously you have a design problem that one way or another needs careful consideration of object lifetimes and ownership. This would be especially true if you want the ability to re-patch filters in the filter graph at runtime, or the ability to create compound FilterChain objects.

    Using shared_ptr will remove most of those issues in one swoop and make your design a lot simpler. The only potential gotcha I think here is if your filter happens to contain cycles. I can see that could happen if you have some kind of feedback loop. In that instance I would suggest having all Filter objects owned by a single class, and then the FilterChain would store weak pointers to the Filter objects.

    I would wager that the execution time of the filter stages would be far in excess of the extra overhead of dereferencing a smart pointer. shared_ptr is designed to be pretty lightweight.

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

Sidebar

Related Questions

Consider the following class structure: public class Foo<T> { public virtual void DoSomething() {
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following data structure: List<Person> People; class Person { List<Car> Cars; List<Hobby> Hobbies;
Consider the following class: public class Event<T> { public delegate void Handler<t>(t msg); private
Consider the following class public class PlanetKrypton { public static void CallSuperManforHelp(string helpMessage, params
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Consider the following class hierarchy: public abstract class Entity { public virtual int Id
Consider the following class: class Something : ISomething { public void DoesSomething(int x) {
Consider the following: class MyClass { public: int operator ()(int a, int b); };
Consider the following class hierarchy: public class Foo { public string Name { get;

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.