How to deallocate memory allocated to a class instance
-
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.cppWorkPackage 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
functionWorkPackage::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
-
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.cppWorkPackage 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
functionWorkPackage::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
What is rear? What is front? What is size? What is data? how is WorkPackage ctor defined?
-
What is rear? What is front? What is size? What is data? how is WorkPackage ctor defined?
rear
andfront
are the indices of a circular array andsize
is queue length.data
is the instance of theWorkPackage
that is being retrieved and returned back for execution. WorkPackage ctorWorkPackage::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
} -
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.cppWorkPackage 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
functionWorkPackage::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
-
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.cppWorkPackage 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
functionWorkPackage::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
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)
-
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)
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);
}