Saturday, September 13, 2014

Token – Shared Pointer Preparation

The first attempt to change token pointers to shared pointers failed (after the change, the code had many memory errors and crashes).  The problems may have been caused by the various containers holding token values being passed as pointers (RPN items in the RPN output list, and items on the translator hold and done stacks).  These containers have now been updated so that their contents are destructed properly and automatically.

The change to shared pointers is being crept up on.  The next step was to replace all references to Token* (pointers to tokens) with the alias TokenPtr:
using TokenPtr = Token *;
This will allow for an easy change to shared pointers.  The changes made during the previous failed attempt were extensive, and it was hard to determine exactly which changes caused the problems.  So the alias change will be separate from the shared pointer change.  The alias change was made first and thoroughly tested (using the memory test script).

Another preparatory change made included passing a constant reference to a token pointer to functions that don't modify the token passed.  This doesn't have much effect for simple pointers, but for shared pointers, it will prevent a copy from being made of the pointer (which incremented its used count, only to be decremented when the argument goes out of scope at the end of the function).

The use of the NULL definition for checking if a token pointer is unset or to assign it to an unset value was changed to the C++11 nullptr, an actual null pointer.  For tokens pointers initialized to a null pointer, the {} C++11 initializer syntax was used.

[branch cpp11 commit 233a8f4017]

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)