C++ Bug: Object is destroyed twice
$30-45 USD
Pago na entrega
When running this code, I get a memory access violation, complaining that the memory area is being deleted twice:
#include "AbstractActivator.h"
// 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>
//
int main()
{
// If I don't create any intances of
// TextDisplayer, I don't get any errors
TextDisplayer show;
// 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.
ActivatorFactory fact;
// Activartors is a typedef for stl::list<Activator>
Activators * acts = fact();
Activators::iterator i;
// Call the activate method of all activators
for(i = acts->begin(); i != acts->end(); ++i)
{
(*i).activate();
}
// 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.
delete acts;
return 1;
}
## Deliverables
1) Explanation of this error is occurring.
2) How to fix it.
3) Modified version of the my source code, with the bug fixed.
## Platform
Windows
ID do Projeto: #2983168