- #1
JonnyG
- 234
- 45
Following my book, we are making a program with two classes: Message and Folder. The idea is that messages will be stored in a folder. The same message can be saved in multiple folders. In order to accomplish this, Message has a set of pointers to Folder that point to each of the folders that contains it. Folder has a set of pointers to Message that point to each of the messages it contains. Here is my code:
My test code that I run in my main.cpp file is (I've removed include directives and other statements for brevity):
This program crashes. Playing with the program, looking at my debugger, and using a memory checker program, it appears the problem is ultimately coming from my remMsg function in the Folder.cpp file. But I can't figure out exactly what's happening. My memory checker gives errors that say unaddressable memory and invalid heap argument, so I know it has something to do with memory getting deleted and dangling pointers, but I can't seem to fix the program.
Any tips?
Edit: I've suspected that when reaching the end of the main function, my Folder object may be getting deleted before my Message object, which leaves the set of pointers that point to my folder dangling (this set is a data member of my Message object). I would need to have access to the Message class members in my Folder destructor to account this possibility, but I cannot #include my Message header in my Folder header, because the Folder header is already #included in my Message header.
Message.hpp:
#ifndef MESSAGE_HPP
#define MESSAGE_HPP
#include <string>
#include <set>
#include "Folder.hpp"
using std::string;
using std::set;
class Message
{
friend class Folder;
public:
explicit Message(const string &str) : contents(str) {}
Message(const Message&);
Message& operator=(const Message&);
~Message();
void save(Folder&);
void remove(Folder&);
private:
string contents;
set<Folder*> folders;
void addToFolders(const Message&);
void removeFromFolders();
void swap(Message&, Message&);
};
#endif
Message.cpp:
#include "Message.hpp"
Message::Message(const Message &msg) : contents(msg.contents), folders(msg.folders) {
addToFolders(msg);
}
Message::~Message() {
removeFromFolders();
}
Message& Message::operator=(const Message &rhs) {
removeFromFolders();
contents = rhs.contents;
folders = rhs.folders;
addToFolders(rhs);
return *this;
}
void Message::save(Folder &f) {
folders.insert(&f);
f.addMsg(this);
}
void Message::remove(Folder &f) {
folders.erase(&f);
f.remMsg(this);
}
void Message::addToFolders(const Message &msg) {
for (auto f : msg.folders)
f->addMsg(this);
}
void Message::removeFromFolders() {
for (auto f : folders)
f->remMsg(this);
}
void Message::swap(Message &lhs, Message &rhs) {
using std::swap;
for (auto f : lhs.folders)
f->remMsg(&lhs);
for (auto f: rhs.folders)
f->remMsg(&rhs);
swap(lhs.folders, rhs.folders);
swap(lhs.contents, rhs.contents);
for (auto f : lhs.folders)
f->addMsg(&lhs);
for (auto f : rhs.folders)
f->addMsg(&rhs);
}
Folder.hpp:
#ifndef FOLDER_HPP
#define FOLDER_HPP
#include <set>
using std::set;
class Message;
class Folder
{
public:
void addMsg(Message*);
void remMsg(Message*);
private:
set<Message*> containedMessages;
};
#endif
Folder.cpp:
#include "Folder.hpp"
void Folder::addMsg(Message *msg) {
containedMessages.insert(msg);
}
void Folder::remMsg(Message *msg) {
containedMessages.erase(msg);
}
My test code that I run in my main.cpp file is (I've removed include directives and other statements for brevity):
main.cpp:
int main() {
Message msg("testing");
Folder inbox;
msg.save(inbox);
}
This program crashes. Playing with the program, looking at my debugger, and using a memory checker program, it appears the problem is ultimately coming from my remMsg function in the Folder.cpp file. But I can't figure out exactly what's happening. My memory checker gives errors that say unaddressable memory and invalid heap argument, so I know it has something to do with memory getting deleted and dangling pointers, but I can't seem to fix the program.
Any tips?
Edit: I've suspected that when reaching the end of the main function, my Folder object may be getting deleted before my Message object, which leaves the set of pointers that point to my folder dangling (this set is a data member of my Message object). I would need to have access to the Message class members in my Folder destructor to account this possibility, but I cannot #include my Message header in my Folder header, because the Folder header is already #included in my Message header.
Last edited: