title: _block_type_is_valid phead- nblockuse on delete command
body: <p>So I know this is a pretty common error, but I'm very new to C++, we are using it for a programing languages class, and this error has me stuck.  The delete parser; line is the line that causes this error, and I cannot figure out why.</p>
  
  <p>Turtle2.cpp</p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;sstream&gt; 
  #include &lt;string&gt; 
  #include &lt;fstream&gt; 
  #include "scanner.h" 
  #include "parser.h"
  
  using namespace std; 
  
  int main(int argc, char* argv[]) { 
  //string line; 
  if (argc != 2) { 
      cout &lt;&lt; "Usage: turtle filename" &lt;&lt; endl; 
      return 0; 
  } 
  char* filename = argv[1];
  cout &lt;&lt; "filename is " &lt;&lt; filename &lt;&lt; endl; 
  ifstream in(filename); 
  Parser * parser = new Parser(&amp;in); 
  AST* tree = parser-&gt;parse(); 
  
  tree-&gt;evaluate(); 
  
  delete tree; 
  delete parser;   //This is the line that causes the error!
  return 0; 
  } 
  </code></pre>
  
  <p>parser.cpp</p>
  
  <pre><code>#include "parser.h"
  #include "calcex.h"
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  Parser::Parser(istream* in) {
  scan = new Scanner(in);
  }
  
  Parser::~Parser() {
  try {
    delete scan;
  } catch (...) {
  }
  }
  
  AST* Parser::parse() {
  AST* returnValue = Prog();
  cout &lt;&lt; "We are still ok1" &lt;&lt; endl;
  return returnValue;
  }
  
  AST* Parser::Prog() {
  AST* result = StmtList();
  Token* t = scan-&gt;getToken();
  
  if (t-&gt;getType() != eof) {
      cout &lt;&lt; "Syntax Error: Expected EOF, found token at column " &lt;&lt; t-&gt;getCol() &lt;&lt; endl;
  throw ParseError;
  }
  cout &lt;&lt; "We are still ok2" &lt;&lt; endl;
  return result;
  }
  
  AST* Parser::StmtList() {
  AST* result = Statement();
  Token* t = scan-&gt;getToken();
  scan-&gt;putBackToken();   
  
  if (t-&gt;getType() != eof) {
    AST* result = StmtList();
          return result;
  }
  
  return result;
  }
  
  AST* Parser::Statement() {
  float num1;
  float num2;
  stmtNode* node;
  Token* t = scan-&gt;getToken();
  /*if (turtle-&gt;getType() != keyword || turtle-&gt;getLex() != "turtle"){
      cerr &lt;&lt;"expected turtle" &lt;&lt; endl;  //print to standard error stream
      throw ParseError;
  }*/
  if (t-&gt;getType() == keyword &amp;&amp; t-&gt;getLex() == "turtle"){
      t = scan-&gt;getToken();
      if (t-&gt;getType() == dot){
          t = scan-&gt;getToken();
          if (t-&gt;getType() == keyword &amp;&amp; t-&gt;getLex() == "goto"){
              t = scan-&gt;getToken();
              if (t-&gt;getType() == lparen){
                  t = scan-&gt;getToken();
                  if (t-&gt;getType() == number){
                      istringstream in(t-&gt;getLex());
                      in &gt;&gt; num1;
                      t = scan-&gt;getToken();
                      if (t-&gt;getType() == comma){
                          t = scan-&gt;getToken();
                          if (t-&gt;getType() == number){
                              istringstream in(t-&gt;getLex());
                              in &gt;&gt; num2;
                              t = scan-&gt;getToken();
                              if (t-&gt;getType() == rparen){
                                  cout &lt;&lt; "Good" &lt;&lt; endl;
                                  node = new stmtNode(num1,num2);
                                  cout &lt;&lt; "X is " &lt;&lt; node-&gt;getX() &lt;&lt; endl;
                                  cout &lt;&lt; "Y is " &lt;&lt; node-&gt;getY() &lt;&lt; endl;
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  else{
      cout &lt;&lt; "bad" &lt;&lt; endl;
  }
  return node;
  }
  </code></pre>
  
  <p>scanner.cpp</p>
  
  <pre><code>#include "scanner.h"
  #include "calcex.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  //Uncomment this to get debug information
  //#define debug
  
  const int numberOfKeywords = 2;
  
  const string keywd[numberOfKeywords] = { //defining the keywords
  string("turtle"), string("goto")
  };
  
  int isLetter(char c) { //c is bigger or equal to 'a' and small or equal to 'z'. same     for captial versions
  return ((c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z'));
  }
  
  int isDigit(char c) { //c is bigger or equal to '0' or smaller or equal to '9' 
  return (c &gt;= '0' &amp;&amp; c &lt;= '9');
  }
  
  int isWhiteSpace(char c) { //c is a space, tab or newline
  return (c == ' ' || c == '\t' || c == '\n');
  }
  
  Scanner::Scanner(istream* in): //scanner constructor
  inStream(in),
  lineCount(1),
  colCount(0),
  needToken(true),
  lastToken(0)
  {}
  
  Scanner::~Scanner() { //scanner de-constructor
  try {
    delete inStream;
  } catch (...) {}
  }
  
  void Scanner::putBackToken() { //method?
  needToken = false;
  }
  
  Token* Scanner::getToken() { //method?
  if (!needToken) {
    needToken=true;
    return lastToken;
  }
  
  Token* t;
  int state=0;
  bool foundOne=false;
  char c;
  string lex;
  TokenType type;
  int k;
  int column, line;
  
  c = inStream-&gt;get();
  
  while (!foundOne) {
    colCount++;
    switch (state) {
       case 0 : 
          lex = "";
          column=colCount;
          line = lineCount;
          if (isLetter(c)) state=1;
          else if (isDigit(c)) state=2;
          else if (c=='.') state=3;
          else if (c==',') state=4;
          else if (c=='(') state=5;
          else if (c==')') state=6;
          else if (c=='\n') {
             colCount=0;
             lineCount++;
          }
          else if (isWhiteSpace(c));
          else if (inStream-&gt;eof()) {
             foundOne=true;
             type=eof;
          }
          else {
             cout &lt;&lt; "Unrecognized Token found at line " &lt;&lt; line &lt;&lt;
                " and column " &lt;&lt; column &lt;&lt; endl;
             throw UnrecognizedToken;
          }
          break;
       case 1 :
          if (isLetter(c) || isDigit(c)) state=1;
          else {
             for (k=0;k&lt;numberOfKeywords;k++)
                if (lex == keywd[k]) {
                   foundOne = true;
                   type = keyword;
                }
             if (!foundOne) {
                type = identifier;
                foundOne = true;
             }
          }
          break;
       case 2 :
          if (isDigit(c)) state=2;
          else {
             type = number;
             foundOne=true;
          }
          break;
       case 3 :
          type = dot;
          foundOne = true;
          break;
       case 4 :
          type = comma;
          foundOne = true;
          break;
       case 5 :
          type = lparen;
          foundOne=true;
          break;
       case 6 :
          type = rparen;
          foundOne=true;
          break;
    }
  
    if (!foundOne) {
       lex = lex + c;
       c = inStream-&gt;get();
    }
   }
  
   inStream-&gt;putback(c);
   colCount--;
   if (type == number || type == identifier || type == keyword) {
    t = new Token(type,new string(lex),line, column);
   }
   else {
    t = new Token(type,new string(lex),line,column);
   }
  
   #ifdef debug
   cout &lt;&lt; "just found " &lt;&lt; lex &lt;&lt; " with type " &lt;&lt; type &lt;&lt; endl;
   #endif
  
   lastToken = t;
   return t;
  
  }
  </code></pre>
  
  <p>I don't know if that is enough code to help you guys out or not, if you need anything more just ask, its not a secret or anything :)  I know its the de-constructor for parser, but I have no idea how to fix it, or really why this is happening...  I've added scanner.cpp, because some ponited out that the problem could be in its deconstructor, so I hope that helps. </p>