带有Splint的MySQL C API:释放字段和行

时间:2014-08-11 16:12:45

标签: mysql c api splint

我正在尝试将Splint与MySQL C API一起使用,并且遇到了一些与释放内存有关的其他问题。在我可以找到关于使用C API的所有示例中,唯一被称为mysql_free_result的释放函数,但永远不会释放行和字段:

while ((row = mysql_fetch_row(result))) 
{ 
  for(int i = 0; i < num_fields; i++) 
  { 
    if (i == 0) 
    {              
       while(field = mysql_fetch_field(result)) 
       {
          // Bla
       }                  
    }        
  } 
}

mysql_free_result(result);
mysql_close(con);

Splint当然会抛出错误,说行和字段是内存泄漏。是吗?怎么解决这个?我应该手动释放行和字段结构吗?

编辑:好的,因为mysql_fetch_row在没有更多行时返回null,我认为它还会释放每个循环中前一行的所有内存。但是我怎么能告诉Splint呢?

编辑2 :这是mysql_fetch_row(版本5.5)的实现。没有分配内存,该功能只是指向下一行。所需要的是一个Splint注释,该函数告诉Splint没有分配内存,但共享。

MYSQL_ROW STDCALL
mysql_fetch_row(MYSQL_RES *res)
{
  DBUG_ENTER("mysql_fetch_row");
  if (!res->data)
  {                     /* Unbufferred fetch */
    if (!res->eof)
    {
      MYSQL *mysql= res->handle;
      if (mysql->status != MYSQL_STATUS_USE_RESULT)
      {
        set_mysql_error(mysql,
                        res->unbuffered_fetch_cancelled ? 
                        CR_FETCH_CANCELED : CR_COMMANDS_OUT_OF_SYNC,
                        unknown_sqlstate);
      }
      else if (!(read_one_row(mysql, res->field_count, res->row, res->lengths)))
      {
        res->row_count++;
        DBUG_RETURN(res->current_row=res->row);
      }
      DBUG_PRINT("info",("end of data"));
      res->eof=1;
      mysql->status=MYSQL_STATUS_READY;
      /*
        Reset only if owner points to us: there is a chance that somebody
        started new query after mysql_stmt_close():
      */
      if (mysql->unbuffered_fetch_owner == &res->unbuffered_fetch_cancelled)
        mysql->unbuffered_fetch_owner= 0;
      /* Don't clear handle in mysql_free_result */
      res->handle=0;
    }
    DBUG_RETURN((MYSQL_ROW) NULL);
  }
  {
    MYSQL_ROW tmp;
    if (!res->data_cursor)
    {
      DBUG_PRINT("info",("end of data"));
      DBUG_RETURN(res->current_row=(MYSQL_ROW) NULL);
    }
    tmp = res->data_cursor->data;
    res->data_cursor = res->data_cursor->next;
    DBUG_RETURN(res->current_row=tmp);
  }
}

1 个答案:

答案 0 :(得分:0)

共享只读存储的正确注释是/*@observer@*/。要共享可写存储空间,可以使用/*@exposed@*/

/*@observer@*/ MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);

Splint manual中的更多内容。