Fix memory leak in lexical parser.
authormjsousa
Wed, 19 Mar 2014 12:05:18 +0000
changeset 879 c61b2e370181
parent 878 89eb85bab58f
child 880 599e88d12f9a
Fix memory leak in lexical parser.
stage1_2/iec_flex.ll
--- a/stage1_2/iec_flex.ll	Sun Mar 16 13:02:28 2014 +0000
+++ b/stage1_2/iec_flex.ll	Wed Mar 19 12:05:18 2014 +0000
@@ -179,17 +179,6 @@
     }
 
 
-/* A counter to track the order by which each token is processed.
- * NOTE: This counter is not exactly linear (i.e., it does not get incremented by 1 for each token).
- *       i.e.. it may get incremented by more than one between two consecutive tokens.
- *       This is due to the fact that the counter gets incremented every 'user action' in flex,
- *       however not every user action will result in a token being passed to bison.
- *       Nevertheless this is still OK, as we are only interested in the relative
- *       ordering of tokens...
- */
-static long int current_order = 0;
-
-
 /* Macro that is executed for every action.
  * We use it to pass the location of the token
  * back to the bison parser...
@@ -207,6 +196,8 @@
 	current_order++;							\
 	}
 
+
+
 /* Since this lexical parser we defined only works in ASCII based
  * systems, we might as well make sure it is being compiled on
  * one...
@@ -510,8 +501,17 @@
 
 
 %{
-#define MAX_INCLUDE_DEPTH 16
-
+
+/* A counter to track the order by which each token is processed.
+ * NOTE: This counter is not exactly linear (i.e., it does not get incremented by 1 for each token).
+ *       i.e.. it may get incremented by more than one between two consecutive tokens.
+ *       This is due to the fact that the counter gets incremented every 'user action' in flex,
+ *       however not every user action will result in a token being passed to bison.
+ *       Nevertheless this is still OK, as we are only interested in the relative
+ *       ordering of tokens...
+ */
+static long int current_order = 0;
+  
 typedef struct {
     int eof;
     int lineNumber;
@@ -522,6 +522,12 @@
     FILE *in_file;
   } tracking_t;
 
+/* A forward declaration of a function defined at the end of this file. */
+void FreeTracking(tracking_t *tracking);
+
+
+#define MAX_INCLUDE_DEPTH 16
+
 typedef struct {
 	  YY_BUFFER_STATE buffer_state;
 	  tracking_t *env;
@@ -984,7 +990,7 @@
 			       */
 			  if (include_stack_ptr == 0) {
 			      // fclose(yyin);           // Must not do this!!
-			      // free(current_tracking); // Must not do this!!
+			      // FreeTracking(current_tracking); // Must not do this!!
 			      /* yyterminate() terminates the scanner and returns a 0 to the 
 			       * scanner's  caller, indicating "all done".
 			       *	
@@ -996,7 +1002,7 @@
 			    yyterminate();
 			  } else {
 			    fclose(yyin);
-			    free(current_tracking);
+			    FreeTracking(current_tracking);
 			    --include_stack_ptr;
 			    yy_delete_buffer(YY_CURRENT_BUFFER);
 			    yy_switch_to_buffer((include_stack[include_stack_ptr]).buffer_state);
@@ -1774,7 +1780,7 @@
 /* Tracking Functions... */
 /*************************/
 
-#define MAX_BUFFER_LENGTH 1000
+#define MAX_LINE_LENGTH 1000
 
 tracking_t *GetNewTracking(FILE* in_file) {
   tracking_t* new_env = new tracking_t;
@@ -1783,12 +1789,18 @@
   new_env->currentChar = 0;
   new_env->lineLength = 0;
   new_env->currentTokenStart = 0;
-  new_env->buffer = (char*)malloc(MAX_BUFFER_LENGTH);
+  new_env->buffer = (char*)malloc(MAX_LINE_LENGTH);
   new_env->in_file = in_file;
   return new_env;
 }
 
 
+void FreeTracking(tracking_t *tracking) {
+  free(tracking->buffer);
+  delete tracking;
+}
+
+
 /* GetNextChar: reads a character from input */
 int GetNextChar(char *b, int maxBuffer) {
   char *p;
@@ -1801,7 +1813,7 @@
     current_tracking->currentTokenStart = 1;
     current_tracking->eof = false;
     
-    p = fgets(current_tracking->buffer, MAX_BUFFER_LENGTH, current_tracking->in_file);
+    p = fgets(current_tracking->buffer, MAX_LINE_LENGTH, current_tracking->in_file);
     if (  p == NULL  ) {
       if (  ferror(current_tracking->in_file)  )
         return 0;
@@ -1920,12 +1932,13 @@
    * We therefore determine how many newlines are in the text we are returning,
    * and decrement the line counter acordingly...
    */
-  /*unsigned int i;
+  /*
+  unsigned int i;
   
   for (i = n; i < strlen(yytext); i++)
     if (yytext[i] == '\n')
-      current_tracking->lineNumber--;*/
-
+      current_tracking->lineNumber--;
+  */
   /* now return all the text back to the input stream... */
   yyless(n);
 }