title: Strange SEGFAULT in C at end of function
body: <p>I have a problem, a strange problem, where I get a segfault, this is the output of GDB:</p>
  
  <pre><code>Core was generated by `./vfirewall-monitor'.
  Program terminated with signal 11, Segmentation fault.
  #0  0x0000000000402a5c in init () at kernel.c:57
  57  }
  (gdb) l
  52      if (read_rules(conn) == NULL) {
  53          return 1;
  54      }
  55      
  56      return 0;
  57  }
  58  
  59  int get_all_system_info() {
  60      /** Inicia a conexão com o banco de dados */
  61  
  (gdb)
  </code></pre>
  
  <p>SEGFAULT appears at 57 line, but this line close the init function.</p>
  
  <p>This is the code of init function:</p>
  
  <pre><code>int init() {
  
      if (read_application_config() == READ_CONFIG_FILE_FAILED)
          return INIT_FAILED;
  
      conn = (DBConnection *) malloc(sizeof (DBConnection));
  
      conn-&gt;dbname = get_config_str(&amp;conf, "dbname");
      conn-&gt;host = get_config_str(&amp;conf, "dbserver");
      conn-&gt;user = get_config_str(&amp;conf, "dbuser");
      conn-&gt;passwd = get_config_str(&amp;conf, "dbpasswd");
      conn-&gt;port = *(get_config_int(&amp;conf, "dbport"));
  
      if (open_connection(conn) == DB_CONNECT_FAILED) {
          insert_log(FATAL, LOG_KERNEL, "Não foi possivel conectar ao banco de dados");
  
          return INIT_FAILED;
      }
  
      Thread thread;
      thread.detach = false;
  
      create_thread(&amp;thread, (void *) get_all_system_info);
  
      if ((int) thread.return_value == GET_ALL_INFO_FAILED)
          return INIT_FAILED;
  
      if (read_rules(conn) == NULL) {
          return 1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>This segfault happened when i call the <code>read_rules()</code> function. This is the code of read_rules()</p>
  
  <pre><code>Rule * read_rules(DBConnection * conn) {
      Query query;
  
      strcpy(query.sql, "SELECT id,table_rule,chain,in_iface,action FROM firewall_rules;");
  
      if (execute_query(conn, &amp;query) == QUERY_EXECUTE_FAILED) {
          insert_log(FATAL, LOG_FIREWALL, "Falha na leitura das regras de firewall - firewall.c");
          return NULL;
      }
  
      Row * row;
      row = fetch(&amp;query);
  
      Rule * rules;
      rules = (Rule *) malloc(sizeof(Rule));
  
  
      if (row == NULL) {
          return NULL;
      }
  
      while (row-&gt;next_line != NULL) {
          printf("Rule: \n");
          printf("ID: %s\n", row-&gt;cell[0]);
          printf("Table: %s\n", row-&gt;cell[1]);
          printf("Chain: %s\n", row-&gt;cell[2]);
          printf("In Iface: %s\n", row-&gt;cell[3]);
          printf("Action: %s\n", row-&gt;cell[4]);
          row = row-&gt;next_line;
      }
  
     clear_query(&amp;query);
  
     free_row(row);
  
      return 0;
  }
  </code></pre>
  
  <p>When i remove the call of read_rules(), the segfault don't happened.</p>
  
  <p>Now the question is: Why is giving problem in closing function?
  Thanks for your attention.</p>