0

I am having some problems when the destructors of my program are called. I have these clases:

  • myModule is the base class where the array of pointers is.
  • myModule_in is a subclass that represents a module with input ports (there is a myModule_out and a myModule_inout that will not be showed here, since it is not necessary).
  • moduleC is one of the input ports modules (moduleA and moduleB are myModule_out and myModule_inout).

myModule:

    class myModule
    {
    public:

      myPort* secondary_ins[NUM];

      myModule() {}

      virtual ~myModule()
      {
        for(int i=0; i<NUM; i++)
        {
          if(secondary_ins[i])
            delete secondary_ins[i];
        }
      }

      virtual void connect(myModule &m) = 0;
    };

myModule_in:

    class myModule_in : public myModule
    {
    public:
      virtual ~myModule_in() {}

      virtual void connect(myModule &m)
      {
        secondary_ins[ins] = new myPort();
        ...
      }

    };

moduleC:

    class moduleC : public myModule_in
    {
    public:

      moduleC();
      ~moduleC() {}

      void connect_modules() {...}

    };

main:

    int main(...)
    {
      moduleA mA;
      moduleB mB;
      moduleC mC;
      ...
      mA.connect(mB);
      mB.connect(mC);
      ...
      return 0;
    }

This code compiles correctly and works fine, until the end of execution, where I get a segmentation fault at: delete secondary_ins[i]; being called from return 0;. The strange thing is that the other modules' destructor are also called and do not have any problem. Maybe there is something wrong with the handling of array of pointers inherited from a base class or something? Any ideas?

Thanks :)

2
  • bit.ly/1dS4wcV Commented Jan 23, 2014 at 16:25
  • Oh! Thanks @735Tesla! I never would have thought of that! Commented Jan 23, 2014 at 16:31

1 Answer 1

4

When you declare:

myPort* secondary_ins[NUM];

nowhere is guaranteed that the array will be 0 initialized. Therefore the test:

if(secondary_ins[i])

in the destructor might pass even if the element was not initialized with a valid pointer. For an easy fix, just initialize with 0 (prior to C++11) or to nullptr (since C++11) the array.

For a real fix, just use a combination of std::vector or std::array and std::unique_ptr.

Sign up to request clarification or add additional context in comments.

2 Comments

I was just writing that I already tried that before and it wasn't working. But I just realized that I was initializing the array in the wrong place. And here, one more question. I was doing it in myModule and it didn't work, so I did it in each of the moduleX that I have (and now it does work). Is it ok? Is there a generic way to do that? Thanks again!
@makeMonday, yes, you use an std::unique_ptr that automatically initializes himself to nullptr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.