When running this code, I get a memory access violation, complaining that the memory area is being deleted twice:
// 3 classes involved:
// TextDisplayer: Base class for a hierarchy of functors that
// display a string in a certain way.
// Activator: Base class for a hierarchy.
// ActivatorFactory: Base class of a hierarchy of functors
// taht return a pointer to a std::list<Activator>
// If I don't create any intances of
// TextDisplayer, I don't get any errors
// ActivatorFactory is a functor that returns
// a pointer to an stl::list<Activator>
// Each Activator instance contains a pointer
// to a TextDisplayer instance. This internal instance
// is deleted in the Activator destructor.
// Activartors is a typedef for stl::list<Activator>
Activators * acts = fact();
// Call the activate method of all activators
for(i = acts->begin(); i != acts->end(); ++i)
// Delete the dynamically created list<Activators>
// This is were I get into trouble. This calls
// the destructor of Activator, that tries to delete
// the internal TextDisplayer. That line creates an
// error, complaining that that memory area has already
// been deleted.
1) Explanation of this error is occurring.
2) How to fix it.
3) Modified version of the my source code, with the bug fixed.