Sunday, October 6, 2013

Constant String Dictionary

The constant string dictionary was a little tricky to get working because of a difficult to resolve memory leak.  The constant string dictionary will also be an information dictionary, but was not defined as an information template dictionary directly.  The information required for this dictionary contains a pointer to a QString instance.  At run-time, an array of QString pointers will be used for the string constants (though the pointers will be wrapped within a class syntactically).

As for number constants, a constant string information class was added with a lone QString pointer to the string constant value.  Even though the information will contain a new instance of a QString, due to the implicit sharing feature of Qt, the underlying string will reference the original string in the token.  The original string will also similarly be referenced by the key list and key map members of the dictionary.  After the token is deleted, the string is still referenced three times (information element, key list and key map).  The string is not deallocated until all three instances are removed.

When the constant string information element is created for a new element, a new QString instance is created from the string in the token.  To prevent a memory leak, these QString instances need to be deleted.  A destructor was needed for the constant string dictionary, so a new constant string dictionary class was created.  This new class overloads the destructor, which deletes all the QString instances in the information elements.  It will be necessary to nullify this QString pointer when the string constant is removed from the dictionary.  The destructor then just deletes all the instance pointers and if one is null then no action is taken.

Initially, an information class instance was created locally in the encode routines and a reference to it was passed to the information dictionary add routine.  For constant strings, a QString instance was created in this local instance.  This was not an issue when the copy of the instance was put into the information vector for a new or reused instance as the instance pointer was copied into the vector.  However, a memory leak occurred when the constant was already in the dictionary and the instance was left hanging (the QString instance was not deleted).  This was previously resolved in the information dictionary template class by having its add routine create the instance only when adding a new item.

To complete the implementation, a constant string dictionary was added to the program unit class with an access function.  The constant string encode and operand text routines were added that simply call the dictionary add and string routines.  Finally, pointers to these functions were added to the associated table entry.  The expected results file for encoder test #1 was updated for the numeric constants that are now in the output.  A new statement was added with a string containing an embedded double quote character.

[commit 8a85668458]

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.)