Saturday, March 30, 2013

Translator RPN List Output

After two failed attempts to create an RPN list class to hold the translator output, smaller incremental changes were made and tested after each change, including the memory test, and then the small changes was committed.

The first small change implemented the new Token::text() member function that returns a string representation of the token.  Previously the Tester::printSmallToken() function performed this task directly to the standard output stream.   It is now necessary to have a string so that it can be output to the program view widget.

The second small change created a new RpnList class based on QList<RpnItem*>, which has all the same functionality as the original list.  This class was defined in a new header file and the RpnItem class definition was moved to this header file from the translator header file.  This class is necessary so that the instance of this class can also hold the error token and message.

The third small change implemented a clear memory function to the new RpnList class to delete the memory used by the RPN item instances created in the translator along with the token instance each item holds.  It is a good idea to have the class perform this task and not every user of the class.

This was where at least one of the problems occurred.  It made sense that the destructor for the RpnList class call this clear function so that the caller would not have clear the list before deleting the instance, but a segmentation fault occurred in the Tester::PrintOutput() function that outputs the text of the RPN List.  The destructor of the instance was called, which deleted all the tokens (it appears the foreach macro is doing this).  When the RPN List instance is then finally deleted, the fault occurred because the token had already been deleted.

This bad change was put on a failedClearAttempt branch [commit a16abf424a] to show what apparently cannot be done.  This will be investigated next.

[commit 9d3d8bc834] [commit f49560c08f] [commit 9522d2a0ad]