[sqlite] Memory leak?

2015-03-16 Thread Matthias Schmitt
Hello,

sorry for the late answer. My weekend was full of events.

> On 13 Mar 2015, at 23:32, Gregory Moore  wrote:
> 
> Are you interacting directly with sqlite?  Or are you using a 3rd party API 
> such as FMDB?  I take it you are not using Core Data (which optionally can 
> use a sqlite database as a datastore.)

I am using FMDB. BTW, this was visible in the call stack I sent in my first 
mail.

> Are you using ARC?  

No. The first versions of my programs are more than 3 years old. At this time I 
made bad experiences with ARC, so I do not like ARC. 

> Have you implemented the -(void)dealloc method for all your Objective-C (or 
> Swift?) objects?

Yes.

> Are you willing to share some of your application code where it interacts 
> with the sqlite database?

No problem with that. Here is a minimum example, which leaks memory.

int numberOfRestos;

FMResultSet *rs = [db executeQuery:@"SELECT count() FROM restaurant"];
while ([rs next]) {
  numberOfRestos = [rs intForColumnIndex:0];
}
[rs close];

I have chosen this example, because it does not involve any complex parameter 
passing or result handling.
This code was working perfectly without memory leaks for years. Now this 
example is leaking two memory segments of ~4.5 KB each.

Interesting fact: I am getting the same memory leaks when I am compiling Sqlite 
from source or if I am using the precompiled library, which comes with the 
Xcode development environment.

Best regards

Matthias Schmitt

magic moving pixel s.a.
23, Avenue Grande-Duchesse Charlotte
L-3441 Dudelange
Luxembourg
Phone: +352 54 75 75
http://www.mmp.lu






[sqlite] Memory leak?

2015-03-13 Thread Matthias Schmitt
Hello,

thank you for your fast response.

> On 12 Mar 2015, at 17:35, Richard Hipp  wrote:
> 
> Let's start with the basics:  How do you know that the memory was in
> fact leaked and is not instead simply being held for reuse?

The Xcode development environment comes with a debugging tool named 
?Instruments? which is able to detect memory leaks quite reliable. It traces 
all references to code segments. If a reference to a code segment is 
overwritten with a new value, but the previously addressed memory segment was 
not released, then the software detects this as a leak. The same thing is true 
when a variable created inside a code block references allocated memory. When 
the program leaves the code block without releasing the previously allocated 
memory, the reference get inaccessible and the allocated memory cannot be 
released any more.

> On 12 Mar 2015, at 17:31, Simon Slavin  wrote:
> Is it, by any chance, always the first SQL command executed after doing 
> sqlite3_open() ?

No, there are multiple memory leaks which add over time.

> Is there any chance you can look at this memory and see what appears there ?

Here is an example. But different leaked memory segments look different.

<0x7fc2938b5600>

5600: 0072 2b9d271d
5608: c9b753a9 bfbc7557
5610: dadb57e5 cffoeefb
5618: db68dd48 09aa68da
5620: aad489da b44f4329
5628: dd3ba2ff 00f0ff00
5630: 3a653236 a9b72d6a
5638: 9d8ceaae 7571942b
5640: 51f251fe ed6bcc73
5648: 3a2d224a 72d43bbd
5650: e9fb96a8 c496a4a8
5658: 57a53a82 96aac489
5660: da9ebd2a 1fbb5657
5668: 6ff1536e e6713d4f
5670: e0ff00c2 ed73e2ff
5678: 008f74df 046883ca

> Do the extra 4344 bytes get included in the number shown by 
> "sqlite3_memory_used()" ?  You could try using that function after 
> sqlite_close() and see what it shows.

I am closing and reopening the database now after every transaction. 
sqlite3_memory_used() shows always 0. I guess that sqlite is telling me by it 
that it is not aware of any lost memory, correct?

> Is the memory released at any later time ?  When you use _close() on the 
> connection, for example ?  Or, if you can track it, when SQLite calls 
> "sqlite3_db_release_memory()" internally ?


The closing and re-opening the database does not change anything in the display 
of ?Instruments?. The program still reports memory leaks. In another attempt I 
tried to compile the code with SQLITE_ENABLE_MEMORY_MANAGEMENT compile-time 
option. Then I tried to call sqlite3_release_memory() after every database 
command. Same result.

Best regards

Matthias Schmitt

magic moving pixel s.a.
23, Avenue Grande-Duchesse Charlotte
L-3441 Dudelange
Luxembourg
Phone: +352 54 75 75
http://www.mmp.lu






[sqlite] Memory leak?

2015-03-12 Thread Matthias Schmitt
Hello,

I know, I am touching a hot iron, when claiming that sqlite might have a memory 
leak. But I am trying to debug memory leaks related to sqlite now since 4 days 
and I am running against walls.
Here is my problem:

- I am writing an iPhone app using iOS 8.2, Xcode 4.2, sqlite 3.8.8.3.

- The memory leak appears in the iPhone simulator more often, than on an 
original iPhone device, but it appears always.

- The leak appears with totally different type of queries. It might be, that a 
leak appears in a specific query in one debug session and in another debug 
session, it doesn?t (which makes it difficult to debug and drives me totally 
nuts). For me it seems to be related to a timing/threading issue. This is why 
it makes not much sense to offer some code here.

- When debugging inside sqlite the leak is always related to the same code 
line, independent from the type of database command (select, insert, delete).

- It appears always with the memory allocation in sqlite.c line 16872 in 
function sqlite3MemMalloc
 Here is the line: void *p = SQLITE_MALLOC( nByte );

- The leaked memory has always a size of nByte = 4344 bytes.

- Here is a typical call stack up to the leaked code:

  0 libsystem_malloc.dylib malloc_zone_malloc
  1 MYAPP sqlite3MemMalloc sqlite3.c:16872
  2 MYAPP mallocWithAlarm sqlite3.c:20510
  3 MYAPP sqlite3Malloc sqlite3.c:20541
  4 MYAPP pcache1Alloc sqlite3.c:39749
  5 MYAPP pcache1AllocPage sqlite3.c:39833
  6 MYAPP pcache1FetchStage2 sqlite3.c:40266
  7 MYAPP pcache1Fetch sqlite3.c:40367
  8 MYAPP sqlite3PcacheFetch sqlite3.c:39119
  9 MYAPP sqlite3PagerAcquire sqlite3.c:46573
 10 MYAPP btreeGetPage sqlite3.c:54380
 11 MYAPP getAndInitPage sqlite3.c:54435
 12 MYAPP moveToRoot sqlite3.c:57233
 13 MYAPP sqlite3BtreeMovetoUnpacked sqlite3.c:57465
 14 MYAPP sqlite3VdbeExec sqlite3.c:74055
 15 MYAPP sqlite3Step sqlite3.c:68839
 16 MYAPP sqlite3_step sqlite3.c:68905
 17 MYAPP -[FMResultSet nextWithError:] FMResultSet.m:155
 18 MYAPP -[FMResultSet next] FMResultSet.m:150
 19 MYAPP __40-[MYAPPAppDelegate loadPreferences]_block_invoke 
MYAPPAppDelegate.m:323
 20 MYAPP __30-[FMDatabaseQueue inDatabase:]_block_invoke FMDatabaseQueue.m:150
 21 libdispatch.dylib _dispatch_client_callout
 22 libdispatch.dylib _dispatch_barrier_sync_f_invoke
 23 MYAPP -[FMDatabaseQueue inDatabase:] FMDatabaseQueue.m:147
 24 MYAPP -[MYAPPAppDelegate loadPreferences] MYAPPAppDelegate.m:321
 25 MYAPP -[MYAPPAppDelegate application:didFinishLaunchingWithOptions:] 
MYAPPAppDelegate.m:157
 26 UIKit -[UIApplication 
_handleDelegateCallbacksWithOptions:isSuspended:restoreState:]
 27 UIKit -[UIApplication 
_callInitializationDelegatesForMainScene:transitionContext:]
 28 UIKit -[UIApplication _runWithMainScene:transitionContext:completion:]
 29 UIKit -[UIApplication workspaceDidEndTransaction:]
 30 FrontBoardServices __31-[FBSSerialQueue performAsync:]_block_invoke
 31 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__
 32 CoreFoundation __CFRunLoopDoBlocks
 33 CoreFoundation __CFRunLoopRun
 34 CoreFoundation CFRunLoopRunSpecific
 35 UIKit -[UIApplication _run]
 36 UIKit UIApplicationMain
 37 MYAPP main main.m:16
 38 libdyld.dylib start

Does anybody have an idea how to debug something like this? Any help would be 
highly appreciated.

Best regards

Matthias Schmitt

magic moving pixel s.a.
23, Avenue Grande-Duchesse Charlotte
L-3441 Dudelange
Luxembourg
Phone: +352 54 75 75
http://www.mmp.lu






[sqlite] Compiler warning: Unused variable ‘pFd'

2014-01-09 Thread Matthias Schmitt
Hello,

I am using SQLite 3.8.2 for my iOS projects in Xcode 5.0.2. After updating to 
version 3.8.2 I am getting the following compiler warning:

sqlite3.c:28141:13: Unused variable ‘pFd’

When looking into the code I see starting at line 28141:

static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
  unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
  UNUSED_PARAMETER(iOff);

#if SQLITE_MAX_MMAP_SIZE>0
  … the code of the function
#endif
  return SQLITE_OK;
}

Obviously the compiler switch “#if SQLITE_MAX_MMAP_SIZE>0” instructs the 
compiler to ignore the code inside the switch on my platform. As a result the 
variable “pFd” is never used and results in an ugly warning message. Would it 
be better to include the variable declaration inside the compiler switch to 
protect from this warning? So the code would look like this:

static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
#if SQLITE_MAX_MMAP_SIZE>0
  unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
  UNUSED_PARAMETER(iOff);
  … the code of the function
#endif
  return SQLITE_OK;
}

Best regards

Matthias Schmitt

magic moving pixel s.a.
23, Avenue Grande-Duchesse Charlotte
L-3441 Dudelange
Luxembourg
Phone: +352 54 75 75 - 0
http://www.mmp.lu




___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users