Page 2 of 2

Re: A task for volunteers: handling out of memory situations

Posted: 24 Nov 2010, 23:02
by StevePoling
Tulon wrote:2. When an out-of-memory situation happens inside the QImage class (part of Qt), it doesn't throw an exception, instead turning the image into a null image. That's very mean if you ask me. This means we would need to insert explicit checks every time we construct a QImage. That's currently done in some places but not in all of them. Fortunately, QImage is rarely used as is. Most of processing inside ST works with either imageproc::BinaryImage or imageproc::GrayImage classes that don't have this problem, even though GrayImage is a wrapper around QImage.
One of the things I've learnt from studying Microsoft code is to check everything outside yourself for indications of failure. in this case, every new should be followed by an is-valid check. The sooner after a failure, the better it is to understand the problem. I liken it to a railroad derailment: all of the train's twisted wreckage is not as interesting as the bad spot of track that goes bad at the very start of problems.
Tulon wrote:These were still good news, if you are wondering. The bad news is that ST is a multi-threaded application, making handling out-of-memory situations difficult. It's not a problem to catch the std::bad_alloc exception in a background thread. The real question is what to do next. You can't inform the user from a non-GUI thread and you can't just eat the exception, because the task that failed probably has someone in another thread expecting its completion.
Have you considered an observer pattern? Exception-throwing threads can send bad news to someplace special. The someone in another thread who is expecting something from its exception-throwing threads can either register for notifications, or poll the bad news repository on a timeout.

I know this is superficial and does not reflect meaningful reflection upon the details you just provided. I've not yet gotten into your code so I let the best parts of what you said in the rest of your post fly over my head. I hope to apply deep thought to them when I'm less ignorant.

Re: A task for volunteers: handling out of memory situations

Posted: 25 Nov 2010, 05:18
by Tulon
StevePoling wrote:One of the things I've learnt from studying Microsoft code is to check everything outside yourself for indications of failure. in this case, every new should be followed by an is-valid check.
That's not the way to go. You use that strategy only if you don't have better options. Exceptions + centralized error handling is the way to go.
StevePoling wrote:Have you considered an observer pattern? Exception-throwing threads can send bad news to someplace special. The someone in another thread who is expecting something from its exception-throwing threads can either register for notifications, or poll the bad news repository on a timeout.
We should use existing inter-thread communication facilities rather than inventing new ones.

Re: A task for volunteers: handling out of memory situations

Posted: 25 Nov 2010, 14:04
by StevePoling
Tulon wrote:
StevePoling wrote:Have you considered an observer pattern? Exception-throwing threads can send bad news to someplace special. The someone in another thread who is expecting something from its exception-throwing threads can either register for notifications, or poll the bad news repository on a timeout.
We should use existing inter-thread communication facilities rather than inventing new ones.
I'm sorry I was not more clear. i wasn't suggesting another inter-thread communication mechanism as much as building on it a solution to your out-of-memory exception in thread A communicating the fault to thread B.