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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:29:06+00:00 2026-06-19T00:29:06+00:00

I have an abstract module class that specific modules derive from. At runtime, I

  • 0

I have an abstract module class that specific modules derive from. At runtime, I parse a config file and determine the specific module type of each module within the config file:

std::vector<module*> modules;
module *temp;

//Let nm be the result of parsing as a std::string

if(nm == "type1")
{
    temp = new module_1(...);
}
else if(nm == "type2")
{
    temp = new module_2(...);
}
else if(nm == "type3")
{
    temp = new module_3(...);
}
else
{
    //Syntax error
    return -1;
}

modules.push_back(temp);
partition p;
p.modules = modules;

handing off the vector modules to a partition class:

class partition
{
    public:
    //Member functions

    private:
    //...Other variables
    std::vector<module*> modules;
};

What’s the proper way to deallocate the memory for these module pointers once I’m done with them? I tried to delete them in the destructor for the partition class as follows, but wound up with a segmentation fault:

partition::~partition()
{
    for(unsigned i=0; i<modules.size(); i++)
    {
         delete modules[i];
    }
}
  • 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-19T00:29:08+00:00Added an answer on June 19, 2026 at 12:29 am

    That depends on how you want to handle ownership and if a partition is going to have value semantics. Think about what should happen if you copy a partition:

    Is the copy of the partition going to share the modules with the original. Are changes in a module to be shared between the partitions?

    If yes, you should use a std::shared_ptr for your partitions. All pain is gone.

    If not, implement a copy-constructor and assignment operator for your partition that performs a deep-copy of the modules list. Implement a destructor that deletes each module in the list. This is safe, because each partition has its own module objects.

    In general, I favor the second approach. If you don’t want to implement the deep-copy, just make partition noncopyable or move-only and use std::unique_ptr to handle the deletion.

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

Sidebar

Related Questions

I have realized my module system by forcing modules to implement an abstract class,
I have a dll that includes: public abstract class Module { internal int ID;
I have a abstract base C# class with a couple of methods that has
I have an abstract Base class. Let's call it Animal::Base . module Animal class
I have a JTree and the Nodes are a abstract class (Item) that extends
Here's the situation: I have an abstract class with a constructor that takes a
I have a Preference class (module) that's used across several different apps. Basically it's
I have a module Shish(which acts like an abstract class) and a visitor class
I have a module with two models that extend Zend_Db_Table_Abstract: class Departments_Model_Subpages extends Zend_Db_Table_Abstract
I have abstract BaseController, which basically looks like below: public abstract class BaseController :

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.