0

请参阅下面在 Monotouch 中使用 Sqlite 的代码。它代表了许多方法中的一种,并且看起来都一样。一切都清理干净了吗?在 Sqlite 文档中,我读到我应该完成每个准备好的语句,但我找不到匹配的调用。那么,我错过了什么吗?

据我所知,我正在打开关闭并正确处理所有内容。尽管如此,我还是随机从 Sqlite收到malloc() 错误。这是最近开始的,所以它也可能是 MT 本身的 Sqlite 版本中的一个问题,因为我的代码没有改变。

using ( SqliteConnection oConn = this.CreateDBMSConnection() )
{
  oConn.Open ();

  using ( SqliteCommand oCmd = new SqliteCommand (
    "SELECT CollaborationItems.*, CollaborationParticipants.intStatus AS intParticipantStatus   FROM CollaborationItems " +
"LEFT JOIN CollaborationParticipants ON CollaborationItems.intItemID = CollaborationParticipants.intItemID " +
"WHERE CollaborationItems.intID = @intD", oConn ) )
  {
    oCmd.Parameters.AddWithValue( "@intID", iItemID );

    using ( SqliteDataReader oReader = oCmd.ExecuteReader (  ) )
    {
      while ( oReader.Read( ) )
      {
         // [...] - some code omitted.

         // The next call again create a DMBS connection, has similar using etc.
         oItem.Participants = this.GetEventParticipants(oItem);
      }
      oReader.Close( );
    }
  }
  oConn.Close( );
}

这是我收到的例外:

Mono.Data.Sqlite.SqliteException: malloc() 在 Mono.Data.Sqlite.SQLite3.Prepare 内存不足 (Mono.Data.Sqlite.SqliteConnection cnn, System.String strSql, Mono.Data.Sqlite.SqliteStatement previous, UInt32 timeoutMS, System.String& strRemain) [0x0022a] 在 /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLite3.cs:343 在 Mono.Data.Sqlite。 SqliteCommand.BuildNextCommand () [0x00019] 在 /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:230

4

1 回答 1

1

内存不足情况的位置意味着您的内存不足 - 但这并不意味着它是分配所有内存的位置。

IOW 失败的地方 可能没有错malloc- 除了没有它请求的可用内存。您可能正在寻找错误的位置来查找(以前的)内存使用情况。

此外,由于sqlite是非托管代码,内存不足的情况不会触发 GC(除非 iOS 向应用程序发送警告)。因此,在此代码中,内存使用量较高的问题更有可能导致应用程序中的许多其他地方出现此故障。这会使您怀疑代码的错误部分。

要找到(或至少确保)您正在寻找正确的位置,我建议您使用 Apple 的 Instruments 并监控应用程序的总内存使用情况。这将告诉哪里(以及何时)分配了大部分内存。

这可能仍然表明,sqlite但即便如此,您应该能够更准确地了解问题。

于 2012-04-18T14:34:03.803 回答