Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to deallocate memory allocated to a class instance

How to deallocate memory allocated to a class instance

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresc++performancehelptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    meerokh
    wrote on last edited by
    #1

    In constructor of my class WorkPackage, I am allocating a stack to each instance of workpackage and adding it to a queue. When a workpackage gets executed, the allocated stack get released in class destructor. The problem is that my destructor gets called before the execution of workpackage and program crashes with segmentation fault. I looked it up and thought that it is because of when i get the package from the queue: PackageQueue.cpp

    WorkPackage PackageQueue::GetWorkPackage(){
    if (isEmpty())
    {
    return WorkPackage(); // return an empty work package
    }
    pthread_mutex_lock(&getlock);
    WorkPackage data=WorkPackageQueue[front];
    if (front == rear)
    {
    rear = -1;
    front = -1;
    }

        else if (front == 0)
            front = size-1;
        else
            front--;
         pthread\_mutex\_unlock(&getlock);
         return WorkPackage(data);
    }
    

    I created a copy contructor to allocate a new stack to the instance in case of deletion after GetWorkPackage function

    WorkPackage::WorkPackage(const WorkPackage& rhs){
    Wp_localstack.local_stack= Stack::make_stack();
    m_action=rhs.m_action;
    m_arguments=rhs.m_arguments;
    }

    But the problem is still there..Need hints to how can i do this correctly. I will provide more code if needed

    V L _ 3 Replies Last reply
    0
    • M meerokh

      In constructor of my class WorkPackage, I am allocating a stack to each instance of workpackage and adding it to a queue. When a workpackage gets executed, the allocated stack get released in class destructor. The problem is that my destructor gets called before the execution of workpackage and program crashes with segmentation fault. I looked it up and thought that it is because of when i get the package from the queue: PackageQueue.cpp

      WorkPackage PackageQueue::GetWorkPackage(){
      if (isEmpty())
      {
      return WorkPackage(); // return an empty work package
      }
      pthread_mutex_lock(&getlock);
      WorkPackage data=WorkPackageQueue[front];
      if (front == rear)
      {
      rear = -1;
      front = -1;
      }

          else if (front == 0)
              front = size-1;
          else
              front--;
           pthread\_mutex\_unlock(&getlock);
           return WorkPackage(data);
      }
      

      I created a copy contructor to allocate a new stack to the instance in case of deletion after GetWorkPackage function

      WorkPackage::WorkPackage(const WorkPackage& rhs){
      Wp_localstack.local_stack= Stack::make_stack();
      m_action=rhs.m_action;
      m_arguments=rhs.m_arguments;
      }

      But the problem is still there..Need hints to how can i do this correctly. I will provide more code if needed

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      What is rear? What is front? What is size? What is data? how is WorkPackage ctor defined?

      M 1 Reply Last reply
      0
      • V Victor Nijegorodov

        What is rear? What is front? What is size? What is data? how is WorkPackage ctor defined?

        M Offline
        M Offline
        meerokh
        wrote on last edited by
        #3

        rear and front are the indices of a circular array and size is queue length. data is the instance of the WorkPackage that is being retrieved and returned back for execution. WorkPackage ctor

        WorkPackage::WorkPackage(void (*action)(void*), void* arguments) {
        Wp_localstack.local_stack= Stack::make_stack();
        m_action = action;
        m_arguments = arguments;
        m_context = make_fcontext(Wp_localstack.local_stack, 1000, m_action);//function to save the context of current work package for context switching purpose.

        WorkPackage dtor

        WorkPackage::~WorkPackage(){
        Stack::release(Wp_localstack.local_stack); //When a workpackage finishes stack allocated to it must be deallocated
        }

        1 Reply Last reply
        0
        • M meerokh

          In constructor of my class WorkPackage, I am allocating a stack to each instance of workpackage and adding it to a queue. When a workpackage gets executed, the allocated stack get released in class destructor. The problem is that my destructor gets called before the execution of workpackage and program crashes with segmentation fault. I looked it up and thought that it is because of when i get the package from the queue: PackageQueue.cpp

          WorkPackage PackageQueue::GetWorkPackage(){
          if (isEmpty())
          {
          return WorkPackage(); // return an empty work package
          }
          pthread_mutex_lock(&getlock);
          WorkPackage data=WorkPackageQueue[front];
          if (front == rear)
          {
          rear = -1;
          front = -1;
          }

              else if (front == 0)
                  front = size-1;
              else
                  front--;
               pthread\_mutex\_unlock(&getlock);
               return WorkPackage(data);
          }
          

          I created a copy contructor to allocate a new stack to the instance in case of deletion after GetWorkPackage function

          WorkPackage::WorkPackage(const WorkPackage& rhs){
          Wp_localstack.local_stack= Stack::make_stack();
          m_action=rhs.m_action;
          m_arguments=rhs.m_arguments;
          }

          But the problem is still there..Need hints to how can i do this correctly. I will provide more code if needed

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then your code is doing something wrong, or in the wrong order. Your destructor needs a method of checking whether the stack has been correctly allocated before trying to release it.

          1 Reply Last reply
          0
          • M meerokh

            In constructor of my class WorkPackage, I am allocating a stack to each instance of workpackage and adding it to a queue. When a workpackage gets executed, the allocated stack get released in class destructor. The problem is that my destructor gets called before the execution of workpackage and program crashes with segmentation fault. I looked it up and thought that it is because of when i get the package from the queue: PackageQueue.cpp

            WorkPackage PackageQueue::GetWorkPackage(){
            if (isEmpty())
            {
            return WorkPackage(); // return an empty work package
            }
            pthread_mutex_lock(&getlock);
            WorkPackage data=WorkPackageQueue[front];
            if (front == rear)
            {
            rear = -1;
            front = -1;
            }

                else if (front == 0)
                    front = size-1;
                else
                    front--;
                 pthread\_mutex\_unlock(&getlock);
                 return WorkPackage(data);
            }
            

            I created a copy contructor to allocate a new stack to the instance in case of deletion after GetWorkPackage function

            WorkPackage::WorkPackage(const WorkPackage& rhs){
            Wp_localstack.local_stack= Stack::make_stack();
            m_action=rhs.m_action;
            m_arguments=rhs.m_arguments;
            }

            But the problem is still there..Need hints to how can i do this correctly. I will provide more code if needed

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            Will need to look at the destructor code. Since you're returning temporary objects, you could try changing the function signature to WorkPackage**&&** PackageQueue::GetWorkPackage

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

            M 1 Reply Last reply
            0
            • _ _Superman_

              Will need to look at the destructor code. Since you're returning temporary objects, you could try changing the function signature to WorkPackage**&&** PackageQueue::GetWorkPackage

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

              Polymorphism in C

              M Offline
              M Offline
              meerokh
              wrote on last edited by
              #6

              Destructor is as follows.

              WorkPackage::~WorkPackage(){
              if (Wp_localstack.local_stack != nullptr)
              {
              Stack::release(Wp_localstack.local_stack);
              }
              }

              and release() is follows.

              void Stack::release(void * stack ){
              int core_id= coreNumber::getInstance()->getCoreID(); //Get the thread id which called the dtor and release
              WPManagerProviderSingleton& singletonObj =
              WPManagerProviderSingleton::getInstance();
              WorkPackageManager &wpm = singletonObj.getWpmInstance(core_id);//Based on thread id, select the workpackage manager(every thread has its own manager) to deallocate the stack from its respective Memory pool
              wpm.PerThreadMemPool.DeAllocate(stack);
              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups