Reputation: 2131
I have an error that some users are having of EXC_BAD_ACCESS
when their device is low on memory. The stack trace is pointing to the if
line below, and I believe it's because of the UTF8String that's being deallocated and still being used:
dispatch_sync(dbQueue, ^{
if (sqlite3_bind_text(sql_stmt, 1, pid.UTF8String, -1, SQLITE_STATIC) != SQLITE_OK) {
...
I'm having a hard time reproducing the issue on my end, how can I force or simulate low-memory on the simulator or a device?
Update:
I've tried adding a breakpoint to the line above, and then using the option Simulator -> Simulate Memory Warning, but I still can't reproduce the EXC_BAD_ACCESS error.
Upvotes: 8
Views: 6024
Reputation: 1750
On the left side of your Xcode screen you can see a button to open the Debug Navigator, there you can see the amount of memory your app is currently using, and the amount that is free.
If you analyze it, you'll realize that the available memory for your simulator is the same as from your computer, so I suggest that you run some application that uses a lot of memory simultaneously with the simulator.
If you have an iPad available it might be easier, what I usually do is go on this website and copy as much of the unicode table as possible, so it will be stored on the Pasteboard
Upvotes: 0
Reputation: 1858
Many times, these kinds of errors are the result of a "perfect storm" of circumstances (i.e. race conditions, infrequent tasks running at just the "right" time, etc.), and often the kind of circumstances that you just cannot anticipate; if you knew how to reproduce it reliably, you'd probably also know how to fix it. The next best thing you can hope for is to try and increase your statistical odds of reproducing it in an environment (the debugger) where you could hopefully make sense of what's happening.
See this post: iOS Development: How can I induce low memory warnings on device?. By simulating memory warnings programmatically, you can (for example) use a repeating timer to cause a memory warning 1/sec (much faster than that and you may run into other issues, which will have you chasing your tail more than solving your original problem), eliminating the need to do it by hand repeatedly.
Before actually running the test, you can also set breakpoints at the following locations:
Symbol Module
====== ======
objc_exception_throw libobjc.A.dylib
-[NSException raise] CoreFoundation
Additionally, set breakpoints on all Objective-C exceptions. Setting a breakpoint will allow you to inspect the contents of memory before the exception actually gets thrown by the runtime, which will give you a much better chance of understanding the problem when it happens. When (and if) you capture the crash, inspect pid
, pid.UTF8String
and sql_stmt
, as those look like the most likely culprits.
Run your app and start the timer firing. This will not necessarily or directly cause the crash you're looking for, but it will probably make it much more likely to occur over time without having to hand-hold; you can fire off the timer and wait (i.e. do something more productive) until you actually see the crash.
Upvotes: 0
Reputation: 3842
In simulator's menu: Hardware-> Simulate Memory Warning.
Update
If you're sure that your app crashed at sqlite3_bind_text, I suppose the most potential problem could be that the pid.UTF8String is NULL sometimes in which case it causes crash. Additionally, it's not likely to be the case that pid or pid.UTF8String is deallocated when used, you can check the crash report (if you have any) and check the address of the memory which caused the EXC_BAD_ACCESS, for example if you have EXC_BAD_ACCESS CODE=2 ADDRESS=0x00000000
, it means pid.UTF8String is indeed a NULL pointer, if the address is not 0x0, then, it's another problem (very unlikely in your case).
As a suggestion, please add nil check to your code:
if (pid) {
if (sqlite3_bind_text(sql_stmt, 1, pid.UTF8String, -1, SQLITE_STATIC) != SQLITE_OK){
// do your stuff
}
} else {
sqlite3_bind_null(sql_stmt,1);
}
Upvotes: 3
Reputation: 112855
In the simulator there is a menu item: Hardware : Simulate Memory Warning
or
shiftcommandM
Upvotes: 4