Bug 22919 - System.Convert.ToBase64String crashes on converting an image file
Summary: System.Convert.ToBase64String crashes on converting an image file
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.4.x
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-09-12 07:09 UTC by jan
Modified: 2016-05-25 01:10 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Comment 1 jan 2014-09-12 08:02:59 UTC
I've updated the description, as the crash seems to be related to the conversion:

DocumentContents = Convert.ToBase64String((byte[])media.File)

and not to Json.NET as previously mentioned.
Comment 2 Sebastien Pouliot 2014-09-12 08:20:33 UTC
Sep 11 13:43:33 Flow-PIlotss-dev-iPad MCSCAiOS[701] <Error>:   at (wrapper managed-to-native) string.InternalAllocateStr (int) <0xffffffff>
Sep 11 13:43:33 Flow-PIlotss-dev-iPad MCSCAiOS[701] <Error>:   at System.Text.StringBuilder.InternalEnsureCapacity (int) [0x000b1] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Text/StringBuilder.cs:716

^ what size is your image ?

Can you attach it to the bug report ?

note: A base64 representation of an image can be quite large (string are unicode, 2 bytes per char, in .NET). You might want to use a stream based approach.
Comment 3 jan 2014-09-12 08:36:07 UTC
The images are about 4-500Kb. For the test purposes it is exactly the same image I am resending.

I know it is not the best way to do it, but unfortunately the backend is out of my control...
Comment 4 Sebastien Pouliot 2014-09-12 08:46:24 UTC
I really suspect your app is running out of memory. Serialization can be very memory hungry process.

> Sep 11 13:43:34 Flow-PIlotss-dev-iPad ReportCrash[703] <Notice>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/MCSCAiOS_2014-09-11-134333_Flow-PIlotss-dev-iPad.ips using uid: 0 gid: 0, synthetic_euid: 501 egid: 0

^ the iOS crash report (once symbolicated) might be helpful. Can you attach it ?

Your (attached) stack trace does not point to `Convert.ToBase64String` so I assume that might happen before ? and the same data is (likely) converted several time (common in serialization) with multiple representation kept referenced in memory.

> but unfortunately the backend is out of my control...

I do not think it really matters to the backend as the same stuff can be sent on the wire in a stream mode (without the same memory requirement). However I can't say if JSON.NET supports that.
Comment 7 Sebastien Pouliot 2014-09-17 09:23:12 UTC
@Jan have you tried to use `sgen` as the garbage collector ? That might help you (within limits).

I do not know RestSharp so I can't tell you if there are "lighter" ways to process such requests. However the current code seems to takes too much memory - and something will need to change (fronted and/or backend) to reduce it.

@Rodrigo, the crash reports ends with:

    	ABORT("Too many heap sections: Increase MAXHINCR or MAX_HEAP_SECTS");

Anything else we might suggest ?
Comment 8 jan 2014-09-17 09:34:12 UTC
@Sebastien, thanks for the quick reply. 

Yes, I've tried 'sgen' but that did not change much. As far as I've found so far, the http body can't be handled as a stream... Idd, something will need to change.
Comment 9 Rodrigo Kumpera 2014-09-17 10:13:00 UTC
That's a boehm limitation that we have no plans on fixing. Switch to sgen.
Comment 10 Sebastien Pouliot 2016-05-25 01:10:27 UTC
Closing wrt comment #9 (bohem). From C8 onward sgen is now the only GC available.

The other part of the issue was not solvable using the API available, requiring too much memory, Sadly this is not something we can solve either.