Crash On String Append

Very simple app that crashes very quickly when trying to (way too enthusiastically) append to a string. No catchable exception seems to be raised.

Tried a few different scenarios (all in the app). On app open, on window open, on button action, on class creation. Always the same result.

Is this known issue? Couldn’t find a case in Feedback that demonstrated this issue.

This crashes in latest beta and latest release version. I was testing on Mavericks(10.9.3). Compiled app crashed in Windows 7.1(VM) as well.

99.999% certainly it “out of memory” since the string grows exponentially

You’re hitting the process’ memory limit when building such a gigantic string. Also, if you’re trying to concatenate many strings together efficiently, I’d recommend using an array that holds each item to be concatenated and using Join at the end.

Right - but I can’t trap the error. That’s the point. It just crashes. (see sample project)

I know it’s a dumb thing to do, but I’d think an exception should be raised. I actually discovered this trying to write an example of a WRONG way to do things.

(thus that sentence)

It seems to me that if your app has gobbled up all the available memory then there is no memory available to do anything else within that app other than to crash.

Not surprising in the least
<https://xojo.com/issue/12072>

When things die unceremoniously in the C++ your app is in a state that catching the error quite literally isn’t possible as that would require memory & you’re process has none to allocate

So what you’re saying is that I should have read the Notes for the OutOfMemoryException :P.

This exception is mostly useful with MemoryBlock and Picture when attempting to allocate large blocks of memory that would exceed the remaining available memory.
Low-level memory situations are inherently problematic, especially if you are frequently requesting small blocks of memory (using MemoryBlock or Picture). Because of this, there is no guarantee that an OutOfMemoryException can even be raised. If the system memory has gotten critically low, the process itself may crash as it attempts to reserve memory in order to display an exception message.
In this situation, you might consider reserving a large block of memory up front and using that within your application rather than repeatedly requesting smaller blocks.

reading the docs is always useful :stuck_out_tongue:

And I made case 33025 to add c++ exception handler to a few calls to make sure that a few simple operations do not cause crashes like a string concat.

I’ve got a couple of Feedback reports on this topic, too. Parsing an email requires multiple slicing and dicing. Works quite well with a 10 MB email. But a 300 MB email made my application crash very quickly. This is one reason why I really want 64bit apps.

I really don’t think a 300 MB email is a good idea.

@Christian: you are so totally funny. Do you want to tell that to my customers? And with Yosemite coming I’ll need to be prepared for GB of attachments.

This discussion about an app that crashes silently because it has exhausted all memory resources prompt me to ask : is there any way in Xojo to know the amount of memory still available to the app ? Knowing that would allow stopping operations before rabbits have filled all cages.

There is a range when low-memory crashes happen. The problem is when to check. Take the string appending for instance. Do you want to check before every string is appended for free memory? Not a good idea for fast algorithms.

When moving data to Filemaker via AppleScript I have a memory leak in Filemaker. I monitor Filemaker’s memory and simply restart Filemaker when the memory gets over a threshhold. But I have it easy there because this is only one routine, which is responsible for managing the data transfer.

[quote=99376:@Beatrix Willius]There is a range when low-memory crashes happen. The problem is when to check. Take the string appending for instance. Do you want to check before every string is appended for free memory? Not a good idea for fast algorithms.

When moving data to Filemaker via AppleScript I have a memory leak in Filemaker. I monitor Filemaker’s memory and simply restart Filemaker when the memory gets over a threshhold. But I have it easy there because this is only one routine, which is responsible for managing the data transfer.[/quote]

I found

enum { gestaltLowMemorySize = 'lmem' };

But if I go system.gestalt("lmem") Xojo complains I am not using enough parameters, although Apple at https://developer.apple.com/library/mac/documentation/Carbon/Reference/Gestalt_Manager/Reference/reference.html apparently does not require any.

How should such key be queried ?

As for when to check, I would believe that it is better to check the threshold before the append, as when memory is maxed, nothing is left to check anything. The crash has already occurred and code will not execute.

I’m using the MBS plugin, but I can’t find the function name right now.

I found the way to check the lmem, ram and lram keys in gestalt. Problem is it seems to pertain to the system and not the app :frowning: