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.
The simulator does a really lousy job of indicating app performance. In fact it doesn't try. For most things the simulator is much, much faster than an iOS device. It runs your code on an x86 processor, which is much faster than the ARM and has many times more memory.
type: ~/Library/Application Support/iPhone Simulator.
in iOS Simulator menu, go to Debug -> Location -> Custom Location. There you can set the latitude and longitude and test the app accordingly.
In the simulator there is a menu item: Hardware : Simulate Memory Warning
or
shiftcommandM
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With