Krumelur
Krumelur

Reputation: 33058

Is there something wrong with the usage of Sqlite in Monotouch in this piece of code? Getting malloc() errors

See the piece of code below that is using Sqlite in Monotouch. It represents one of many methods and all kind of look the same. Is everything cleaned up as it should be? In the Sqlite docs I read that I should finalize every prepared statement but I cannot find a matching call. So, am I missing something?

As far as I can see, I'm opening closing and disposing everything correctly. Nevertheless I'm getting malloc() errors from Sqlite randomly. This has started recently so it could also be a problem in the Sqlite version of MT itself as my code has not been changed.

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( );
}

This is the exception I receive:

Mono.Data.Sqlite.SqliteException: malloc() failed out of memory at Mono.Data.Sqlite.SQLite3.Prepare (Mono.Data.Sqlite.SqliteConnection cnn, System.String strSql, Mono.Data.Sqlite.SqliteStatement previous, UInt32 timeoutMS, System.String& strRemain) [0x0022a] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLite3.cs:343 at Mono.Data.Sqlite.SqliteCommand.BuildNextCommand () [0x00019] in /Developer/MonoTouch/Source/mono/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0/SQLiteCommand.cs:230

Upvotes: 0

Views: 1757

Answers (1)

poupou
poupou

Reputation: 43553

The location of an out of memory condition means that's where you ran out of memory - but it does not means it is where all the memory was allocated.

IOW there's likely nothing wrong where malloc fails - beside not having the available memory it request. You might be looking at the wrong place to find your (previous) memory usage.

Also since sqlite is unmanaged code a low memory condition will not trigger the GC (unless iOS sends a warning to the application). So a high memory usage bump, in this code, is more likely to cause this failure that many other places in your application. This can make you suspect the wrong part of the code.

To find (or at least ensure) you're looking at the right place I suggest you to use Apple's Instruments and monitor the application total memory usage. That will tell where (and when) most of the memory is allocated.

That might still point to sqlite but, even then, you should be able to get a far more precise picture of the issue.

Upvotes: 1

Related Questions