473,395 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

destructor dependency while return from a function

tom
I have the section of code listed below, but when I return from main
an exception is thrown from the library, because while returning,
destructor is called on each of the object in the sequence below:
step1: destroy m2
step2: destroy f
step3: destroy m1
notice, "m1" and "m2" have the class type of Message, and will both
use object "f" in the destructor. The problem happens when run to
step3, it uses an already destroyed object f. (dangling pointer).

My question is: Is there any known solution or design pattern to solve
this problem?

#include <iostream>
#include <vector>
#include <hash_map>
#include <cctype>
#include <cassert>
#include <fstream>
#include <sstream>
#include <list>
#include <deque>
#include <algorithm>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
class Message;

class Folder
{
public:
void add_Msg(Message*);
void remove_Msg(Message*);
~Folder()
{
std::cout<<"~Folder()"<<std::endl;
}
private:
std::set<Message*messages;
};

void Folder::add_Msg(Message* m)
{
messages.insert(m);
}

void Folder::remove_Msg(Message* m)
{
messages.erase(m);
}

class Message
{
public:
Message(const std::string &str = ""):contents(str){}
Message(const Message&);
Message& operator=(const Message&);
~Message();
void save(Folder&);
void remove(Folder&);
private:
std::string contents;
std::set<Folder*folders;
void put_Msg_in_Folders(const std::set<Folder*>&);
void remove_Msg_from_Folders();
};

Message::Message(const Message& m):contents(m.contents),
folders(m.folders)
{
put_Msg_in_Folders(m.folders);
}

Message& Message::operator=(const Message& m)
{
remove_Msg_from_Folders();
put_Msg_in_Folders(m.folders);
contents = m.contents;
folders = m.folders;

return *this;
}

Message::~Message()
{
remove_Msg_from_Folders();
}

void Message::save(Folder& f)
{
folders.insert(&f);
f.add_Msg(this);
}

void Message::remove(Folder& f)
{
folders.erase(&f);
f.remove_Msg(this);
}

void Message::put_Msg_in_Folders(const std::set<Folder*>& f)
{
std::set<Folder*>::const_iterator iter = f.begin();
while(iter!=f.end())
{
(*iter)->add_Msg(this);
++iter;
}
}

void Message::remove_Msg_from_Folders()
{
std::set<Folder*>::iterator iter = folders.begin();
std::cout<<reinterpret_cast<int>(*iter)<<std::endl ;
while(iter!=folders.end())
{
(*iter)->remove_Msg(this);
++iter;
}
}

int main(int argc, char *argv[])
{
Message m1("s");
Folder f;
m1.save(f);
Message m2(m1);
m2 = m1;
return 0;
}

Aug 23 '07 #1
1 1642
On Aug 24, 12:33 pm, tom <pxk...@gmail.comwrote:
>
Thanks a lot for your answer. Your advice rocks.
To deal with problem, I think I could only fall back to dynamic memory
allocation and reference counter or smart pointer.(Though it's not a
good idea to put it in a container here in this scenario)- Hide quoted text -
To put it another way, when programming with containers it's always a
good idea to have a clear picture of who *owns* the contained data
(as opposed to merely referencing it). With this in place, you can
ensure that destruction happens correctly without having to revert to
smart pointers/reference counting. Many folks will tell you that it's
better/safer to use smart pointers anyway, but often just getting
this
ownership issue straight in your head (and in the code!) is
sufficient.

Aug 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
17
by: Phlip | last post by:
C++ers: I have this friend who thinks C++ cannot throw from a destructor. I think throwing from a destructor is bad Karma, and should be designed around, but that C++ cannot strictly prevent...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
3
by: Juha Nieminen | last post by:
This is a simplified version of the situation (the actual situation is quite more complex, but when stripped to the bare minimum, it's like this): class BaseClass { public: virtual void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.