Objective-C - How to make low memory on an iOS simulator

I have an error that some users have EXC_BAD_ACCESS when their device is low in memory. The stack trace points to the if line below, and I think this is because the UTF8String is freed and used:

 dispatch_sync(dbQueue, ^{ if (sqlite3_bind_text(sql_stmt, 1, pid.UTF8String, -1, SQLITE_STATIC) != SQLITE_OK) { ... 

It's hard for me to reproduce the problem at my end, how can I make or simulate small memory on a simulator or device?

Update:

I tried adding a breakpoint to the line above, and then using the Simulator → Simulate Memory Warning option, but I still cannot reproduce the EXC_BAD_ACCESS error.

+7
ios memory
source share
4 answers

The simulator has a menu item: Hardware: Simulate a memory warning

or

shift command M

+4
source share

In the simulator menu: Hardware-> Simulate a memory warning .

Update

If you are sure that your application crashed into sqlite3_bind_text, I believe that the most likely problem may be that pid.UTF8String is NULL, sometimes in this case it causes a crash. In addition, this is unlikely to occur when pid or pid.UTF8String is freed when used, you can check the crash report (if any) and check the memory address that caused EXC_BAD_ACCESS, for example, if you have EXC_BAD_ACCESS CODE=2 ADDRESS=0x00000000 , this means that pid.UTF8String is really a NULL pointer, if the address is not 0x0, then this is another problem (very unlikely in your case).

As a suggestion, add zero 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); } 
+3
source share

On the left side of the Xcode screen, you can see a button to open the Debug Navigator, there you can see the amount of memory that your application uses and the amount that is free.

If you analyze this, you will realize that the available memory for your simulator is the same as on your computer, so I suggest you run some application that uses a lot of memory at the same time as the simulator.

If your iPad is available, it may be easier what I usually do on this website and copy as much of the Unicode table as possible, so it will be saved on the Pasteboard

0
source share

Many times, such errors arise as a result of an “ideal storm” of circumstances (ie race conditions, infrequent tasks that are performed only at the “right” time, etc.), and often circumstances that you simply cannot foresee; if you knew how to reliably reproduce it, you probably also know how to fix it. The next thing you can hope for is to try to increase the statistical chances of reproducing it in an environment (debugger), where you could hope to understand what is happening.

See this post: iOS Development: how can I trigger low memory alerts on a device? . By simulating software memory warnings, you can (for example) use a repeating timer to trigger a 1 / second memory warning (much faster than this, and you may run into other problems that will cause you to chase your tail more than solve your original problem) , eliminating the need to do this manually.

Before actually running the test, you can also set breakpoints in the following places:

 Symbol Module ====== ====== objc_exception_throw libobjc.A.dylib -[NSException raise] CoreFoundation 

Also, set breakpoints in all Objective-C exceptions. Setting a breakpoint allows you to check the contents of the memory before the exception is actually thrown by the runtime, which will give you a much better chance of understanding the problem when this happens. When (and if) you commit a crash, check pid , pid.UTF8String and sql_stmt , as they look like the most likely culprits.

Launch the application and turn on the timer. This will not necessarily or directly lead to the failure you are looking for, but most likely it will become more likely over time without having to hold hands; you can turn off the timer and wait (that is, do something more productive) until you see a collision.

0
source share

All Articles