Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » How to handle a lack of memory
How to handle a lack of memory [message #46272] Wed, 06 April 2016 10:47 Go to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello all

Now String::Cat() as many U++ functions calls SysAllocRaw() to allocate a String.

However if requested size is too big, SysAllocRaw() unconditionally shows a panic message and stops the program.

I propose you to throw an exception in this case, so that the main program can handle this properly.

In the old days we did this Smile :
char *a = malloc(size);
if (a == 0) {
	printf("\nNot enough memory available");
	... code that handles this properly ...
	... program can follow running without crashing ...
}


Best regards
Iñaki
Re: How to handle a lack of memory [message #46281 is a reply to message #46272] Thu, 07 April 2016 22:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
It is not so easy...

Frankly, it all boils down to VectorMap::Add implementation as example:


T& Add(const K& k, const T& x) { key.Add(k); return value.Add(x); }

Now if value.Add runs out of memory, VectorMap is left in inconsistent state. Not that this is just example, practical code is full of examples like this. E.g. what is wrong with this code:

struct Foo {
   Bar *ptr;

   Foo() { ptr = new Bar; }
   ~Foo() { delete ptr; }
}


It is certainly possible to solve that by adding exception block into Add, but correctly resolving this issue everywhere would result in significant increase in codebase (and probably decreased performace). And it is virtually untestable.

Plus, in 64 bit platform (but often with 32-bits too), you are rather going to freeze system first (because of swap trashing) that actually get to out-of-memory.

In practice, I do not remember getting out-of-memory in other situations that program bugs. Have you ever got out-of-mem in theide?

Mirek

[Updated on: Thu, 07 April 2016 23:00]

Report message to a moderator

Re: How to handle a lack of memory [message #46285 is a reply to message #46281] Fri, 08 April 2016 09:00 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello Mirek

For example there is a program with a virtual ArrayCtrl that takes data from a huge file.
If user selects and copy the data to the Clipboard, program may crash.


Best regards
Iñaki
Re: How to handle a lack of memory [message #46288 is a reply to message #46285] Fri, 08 April 2016 09:09 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
koldo wrote on Fri, 08 April 2016 09:00
Hello Mirek

For example there is a program with a virtual ArrayCtrl that takes data from a huge file.
If user selects and copy the data to the Clipboard, program may crash.


Well, I have to say that generally, this is one of areas where I am not really that much sure that used approach (Panic) is correct.

However, somewhat I suspect that here the sensible solution is rather checking the size of selection before starting the process. The advantage is that it would solve "freezing" problem too.

I know that it is not always possible. And sometimes such check is difficult - but making the code "memory exception safe" reliably is much more difficult than that, I am afraid...

Mirek
Re: How to handle a lack of memory [message #46298 is a reply to message #46288] Mon, 11 April 2016 08:33 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello Mirek

I agree with you. However I think that this issue should have to be solved some time.
Maybe it is not possible to solve it soon for all cases, but maybe it is possible to solve it for a big percentage of them.
Remember the Pareto principle (for example, with 20% of the effort you may solve the 80% of the problem) Smile.


Best regards
Iñaki

[Updated on: Mon, 11 April 2016 08:35]

Report message to a moderator

Re: How to handle a lack of memory [message #46300 is a reply to message #46298] Mon, 11 April 2016 12:06 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
BTW, for reference, Qt is not much advanced in handling this problem either:

http://doc.qt.io/qt-4.8/exceptionsafety.html

IMO: Really not worth spending time with this. I could imagine replacement function for Panic, but I do not think that it is worth the effort to make the code "bad_alloc exception safe".
Previous Topic: Please help! New transfer semantic issue!
Next Topic: [FIXED] ToUpper, ToLower,and ToAscii (char*, int) causes AssertFailed
Goto Forum:
  


Current Time: Thu Mar 28 10:17:02 CET 2024

Total time taken to generate the page: 0.04240 seconds