CGContextDrawPDFPage crashing without ever producing a memory warning - ios

I'm trying to render a PDF page with some annotations on it (to email), and most of the time this works fine.
However, with this PDF in particular it seems to crash every time on the call to CGContextDrawPDFPage for the first page. I have added
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
as a recommendation from CGContextDrawPDFPage taking up large amounts of memory but that did not seem to solve my issue.
I ran this through the profiler to see what was going on, and it slowly started allocating a bunch of memory until finally it hit 512MB of live bytes and died.
The strange thing is that when I render this to an image (for display on the device) context it seems to work fine but when I render it to a PDF context (for emailing with annotations), it breaks. All of the other PDFs I've tried work fine in both cases.
I was hoping that someone who's written a PDF viewer/annotator could test this out and see if this document works for them and if so, give me some tips about what they're doing to reduce the memory allocations of CGContextDrawPDFPage.
Another resource I looked at was this great SO Question.
Edit (more info on the crash):
90% of the time the crash doesn't actually show up, the gdb line displays but the app goes away and the stack trace doesn't show. (It says paused but there is no thread display). One time I saw it was an exc_bad_access and it showed this in the log:
malloc: *** mmap(size=16777216) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

Are you constructing the PDF on the main thread? If so, you may not receive the memory warning because you've blocked the signal while in execution, since the run loop never has a chance to dispatch the message to you.

Remove the exception breakpoint in xcode and it works fine.

Related

Application hangs with Full page heap enabled

My Application crashes while closing a Dialog. I tried to debug the code and could not make any good conclusions from that. I took crash dumps and analyzed using the Debug Diag tool and got the report that it could be a Heap corruption in MSVBVM60.dll.
As a next step, I tried enabling the Full Page heap but my application completely hangs. I am aware that enabling Full page heap would bring down the performance and can make the application not Responsive. This exactly happened with my application as well.
I tried with Normal Page heap, but still did not get any good info.
I tried setting in the Application verifier and also in the GFlags.
I tried for Heap tail and other options but could not really conclude the root cause.
I Tried for the specific image and not the whole machine.
Is it possible to take Full page heap by increasing the RAM of my machine?
Or is there any thing that I can do to get Full page heap? Please suggest.

'Termination due to memory pressure" in UIWebView

I have started receiving app termination due to memory pressures very recently (mostly with iOS 7). The app used to work very well on iOS 6.
The app is mostly uses a UIWebView to display a webpage. However for a few webpages it creates ever increasing Dirty memory with most of it due to Image IO and Performance tool data (see attached instruments screenshots). I am completely clueless as to where to dig next. Could someone guide me what should I do here onwards? In the second screenshot I see a sudden bump in the memory allocations. Is there a way to spot the process/part of the code which caused it?
EDIT One can reproduce these results in a simple demo kept at www.github.com/nikhiljjoshi/iosCrash (just change the site to www.nzz.ch).
Thanks in advance,
Nikhil

Memory issue with large NSData

I was reading a really large file into memory on the main thread (not just the simple dataWithContentsOfFile: method, I was also doing some data manipulation), and my app crashed. After I moved this process to a background thread, the app doesn't crash anymore. The only thing that happens is that the NSData I get is nil.
Is it possible that only my background thread is interrupted without affecting the main thread, or am I missing something? Also, can I count on this behaviour and just display an error message when the NSData is nil, without the app to crash? Thank you in advance.
Edit:
The log I get is:
malloc: * mmap(size=629800960) failed (error code=12)
error: can't allocate region
** set a breakpoint in malloc_error_break to debug
The file I was using is about 250 MB large.
I know why it crashed on the main thread. My question is, why didn't it crash on a secondary thread?
I guess it can depend on the crash, but if it is a problem of lack of memory (you can both be using too much memory or be creating too many objects) it will crash the entire app.
You should try to test the app with smaller files and see how it behaves.

how to load more cells in tableView

i am writing a iPad application and displaying the tableView in my application. I have two options in tableview:
includeAll
ExcludeAll
Once i click the IncludeAll all cells should be selected. I am loading more then 10000 records. once i select the include all i got one error message.
error message is
malloc: *** mmap(size=2097152) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Googling will reveal quite a few tutorials on using instruments to understand what is going on with your memory:
How to debug memory leaks: (tutorial)
http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial
And another:
Finding Obj-C memory leaks (video)
http://www.youtube.com/watch?v=R449qEuexNs&feature=related
Probably because your simulator is able to allocate ~500Mb of memory while your iPad is not able to do it. I think you should rethink what you are doing
do you really need so much memory?
isn't it just a calculating bug? (maybe a wrong sizeof or whatever)
in any case this is really too much data to be handled

iPhone memory warnings and crashes - but Instruments showing lowish memory use

I have a strange memory issue I'm having problems resolving and would appreciate some advice as to where else to look.
The program I have (iPhone App) has a function whereby it basically downloads loads of files, processes those that are JSON, and stores the rest to disk. The JSON processing is CPU intensive and can take several seconds per file, so I have a NSOperationQueue with maxConcurrency limited to 1 that handles all the heavy lifting, and a queue that manages the multiple files to download.
Ever since iOS5 came out, the App has had problems completing the download sequence without crashing and so far what I have tried is;
1) Changed the performSelectorOnBackgroundThread JSON processing to use a single NSOperationQueue so as to limit the number of background threads working with large objects.
2) Added NSAutoReleasePools inside loops that create multiple, large, transient objects.
3) Flushed the sharedURLCache to ensure the files aren't hanging around in the system cache.
4) Stored the JSON objects to disk using NSKeyedArchiver and passed the filenames between threads rather than the actual objects, to again try to mitigate the number and size of retained objects currently in use.
All of these at first seemed to make a difference, and when I look at the memory allocations, I've now got the peak usage down from just over 20MB (hence no wonder it was crashing) to under 10MB, and yet the app is still crashing with low memory as before.
I'm trying to trace what is eating the memory causing the app to crash and on this occasion I'm having real problems persuading Instruments to tell me anything useful.
Here's a typical trace (on an iPhone 3GS running iOS 4.3.5)
You can see that the PEAK usage was a tad over 7MB and yet shortly after, you can see the 2 flags pertaining to low memory, and then low memory urgent, followed by the app terminating shortly thereafter.
If I use the memory monitor, the cause of the crash seems clear enough - physical memory is being exhausted - look at the light green trace below. The low memory warnings co-incide (not surprisingly) with the physical memory running out.
There are no leaks showing FWIW either (I've done that in other runs).
It's not image caches or NSURLConnection caches and the only thing I can think of is that perhaps there are some bad leaks that aren't being detected ... but I'm having issues identifying them because if I click into all allocations to see the objects that are live, and then do a command-A to select them all (in order to paste them into a spreadsheet to see where the memory seems to be), at the point I hit command-C to copy them, Instruments beachballs and never recovers.
I really cant figure out what's going on. Does anyone have some advice on how to persuade instruments to show me some more useful information about what is using this memory?
Sorry I can't post any meaningful code fragments ... hopefully the instruments screenshots at least give you an idea about where I'm coming from.
The Leaks instrument isn't terribly useful for figuring out anything but the obvious leaks in your app.
What you are describing is an ideal candidate for heapshot analysis.
tl;dr Heapshot analysis allows you to see exactly how the heap of your application grows between any two points of time (where you determine the points).

Resources