title: Valgrind debug log: Invalid read of size 8
body: <p>recently I decide to debug my application with valgrind.
  I've resolved a lot of errors, but can't this one.</p>
  
  <pre><code>==12205== Invalid read of size 8
  ==12205==    at 0x37E1864C40: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (in /usr/lib64/libstdc++.so.6.0.8)
  ==12205==    by 0x40393C: readConfig(std::string) (stl_tree.h:257)
  ==12205==    by 0x4058BE: main (application.cpp:42)
  ==12205==  Address 0x5589b88 is 24 bytes inside a block of size 48 free'd
  ==12205==    at 0x4A05A33: operator delete(void*) (vg_replace_malloc.c:346)
  ==12205==    by 0x4067AD: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, std::string&gt;, std::_Select1st&lt;std::pair&lt;std::string const, std::string&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;::erase(std::_Rb_tree_iterator&lt;std::pair&lt;std::string const, std::string&gt; &gt;, std::_Rb_tree_iterator&lt;std::pair&lt;std::string const, std::string&gt; &gt;) (new_allocator.h:94)
  ==12205==    by 0x406841: std::_Rb_tree&lt;std::string, std::pair&lt;std::string const, std::string&gt;, std::_Select1st&lt;std::pair&lt;std::string const, std::string&gt; &gt;, std::less&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;::erase(std::string const&amp;) (stl_tree.h:1215)
  ==12205==    by 0x403934: readConfig(std::string) (stl_map.h:461)
  ==12205==    by 0x4058BE: main (application.cpp:42)
  </code></pre>
  
  <p>Part of my code:</p>
  
  <pre><code>string config_file;
  if (strlen(getParam("config") . c_str()) &gt; 0)
  {
      config_file = getParam("config");
  }
  else
      config_file = string("default.conf");
  if (access(config_file . c_str(), 0) == -1)
  {
      printf("Config file \"%s\" not exists\n", config_file . c_str());
      exit(1);
  }
  if (!readConfig(config_file))
  {
      printf("Application error: read config file\n");
      exit(1);
  }
  </code></pre>
  
  <p>String #42:</p>
  
  <pre><code>if (!readConfig(config_file))
  </code></pre>
  
  <p>Please try to help me.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Update #1:</p>
  
  <p>I apologize for so large function :(</p>
  
  <pre><code>bool readConfig(string filename)
  {
   time_t rawtime;
   struct tm * timeinfo;
   time ( &amp;rawtime );
   timeinfo = localtime ( &amp;rawtime );
   map&lt;string,string&gt; tmp_buff;
  
    ifstream ifs( filename.c_str() );
     string temp,tmp;
     int i;
     unsigned int MAX_MATCH = 40;
     regex_t re;
     char *pattern = "([^=]+)=(.+)";
  
     const char* s;
     map&lt;int,string&gt; matches_tmp;
     map&lt;string,string&gt;::const_iterator it;
     char s1[1024];
     size_t rm;
     regmatch_t pmatch[MAX_MATCH];
     regcomp(&amp;re, pattern, REG_ICASE|REG_EXTENDED|REG_NOSUB);
     if ((rm = regcomp (&amp;re, pattern, REG_EXTENDED)) != 0)
      {
        printf("Invalid expression:'%s'\n",pattern);
        return false;
      }
     int start[2]={0},end[2]={0},current[2]={0};
     char * b;
     string substr;
     bool start_time=false,inside_time=false,error=false;
  
    while( getline( ifs, temp ) )
    {
        tmp=trim(temp);
        tmp=temp;
        if(strlen(tmp.c_str())==0) continue;
        s=tmp.c_str();
  
  
        if(!regexec(&amp;re, s, MAX_MATCH, pmatch, 0))
        {
           for(i=1;i&lt;=2;i++)
           {
              strncpy (s1, s + pmatch[i].rm_so, pmatch[i].rm_eo - pmatch[i].rm_so);
              s1[pmatch[i].rm_eo - pmatch[i].rm_so] = '\0';
              matches_tmp[i]=trim((string)s1);
           }
  
           if(matches_tmp[1]==string("start-time"))
           {
              substr=matches_tmp[2].substr(0,2);
              b=new char[substr.length()+1];
              strcpy(b, substr.c_str() );
  
  
              if(strlen(b)!=2) continue;
              start[0]=atoi(b);
              //free(b);
  
              substr=matches_tmp[2].substr(3,2);
              b=new char[substr.length()+1];
              strcpy(b, substr.c_str() );
  
              if(strlen(b)!=2) continue;
              start[1]=atoi(b);
              start_time=true;
              continue;
           }
  
           if(matches_tmp[1]==string("end-time"))
           {
              start_time=false;
  
              substr=matches_tmp[2].substr(0,2);
              b=new char[substr.length()+1];
              strcpy(b, substr.c_str() );
  
  
              if(strlen(b)!=2) error=true;
              end[0]=atoi(b);
  
              substr=matches_tmp[2].substr(3,2);
              b=new char[substr.length()+1];
              strcpy(b, substr.c_str() );
  
              if(strlen(b)!=2) error=true;
              end[1]=atoi(b);
  
  
              if(error)
              {
                 printf("ERROR1\n");
                 error=false;
                 continue;
              }
  
              current[0]=timeinfo-&gt;tm_hour;
              current[1]=timeinfo-&gt;tm_min;
  
              if(end[0]&lt;start[0])
              {
                 if(
                       (current[0]&lt;start[0] &amp;&amp; current[0]&gt;end[0]) ||
                       (current[0]==start[0] &amp;&amp; current[1]&lt;start[1]) ||
                       (current[0]==end[0] &amp;&amp; current[1]&gt;end[1])
                   )
                   {
                       error=true;
                   }
              }else
              {
                 if(
                       (current[0]&lt;start[0]) ||
                       (current[0]&gt;start[0] &amp;&amp; current[0]&gt;end[0]) ||
                       (current[0]==start[0] &amp;&amp; current[1]&lt;start[1]) ||
                       (current[0]==end[0] &amp;&amp; current[1]&gt;end[1])
                    )
                 {
                    error=true;
                 }
              }
  
              if(error)
              {
                 error=false;
                 continue;
              }
  
              for (it = tmp_buff.begin(); it != tmp_buff.end(); ++it)
              {
                 if(config.find( it-&gt;first ) != config.end()) config.erase(it-&gt;first);
                 config[it-&gt;first]=it-&gt;second;
                 tmp_buff.erase(it-&gt;first);
              }
  
           }
           if(strlen(matches_tmp[1].c_str())==0) continue;
           if(start_time)
           {
              tmp_buff[matches_tmp[1]]=matches_tmp[2];
           }
           else
              config[matches_tmp[1]]=matches_tmp[2];
        }
    }
  }
  </code></pre>