Skip to content

Commit e69c568

Browse files
committed
Handle out-of-memory situations, giving users the opportunity to save their project.
1 parent 1f295a8 commit e69c568

5 files changed

+106
-82
lines changed

Application.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Scan Tailor - Interactive post-processing tool for scanned pages.
3-
Copyright (C) 2007-2009 Joseph Artsimovich <joseph_a@mail.ru>
3+
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -16,9 +16,23 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include "Application.h"
1920
#include "Application.h.moc"
21+
#include "OutOfMemoryHandler.h"
22+
#include <new>
2023

2124
Application::Application(int& argc, char** argv)
2225
: QApplication(argc, argv)
2326
{
24-
}
27+
}
28+
29+
bool
30+
Application::notify(QObject* receiver, QEvent* e)
31+
{
32+
try {
33+
return QApplication::notify(receiver, e);
34+
} catch (std::bad_alloc const&) {
35+
OutOfMemoryHandler::instance().handleOutOfMemorySituation();
36+
return false;
37+
}
38+
}

Application.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Scan Tailor - Interactive post-processing tool for scanned pages.
3-
Copyright (C) 2007-2009 Joseph Artsimovich <joseph_a@mail.ru>
3+
Copyright (C) Joseph Artsimovich <joseph.artsimovich@gmail.com>
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -26,6 +26,8 @@ class Application : public QApplication
2626
Q_OBJECT
2727
public:
2828
Application(int& argc, char** argv);
29+
30+
virtual bool notify(QObject* receiver, QEvent* e);
2931
};
3032

3133
#endif

OutOfMemoryDialog.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ OutOfMemoryDialog::OutOfMemoryDialog(QWidget* parent)
3030
: QDialog(parent)
3131
{
3232
ui.setupUi(this);
33+
if (sizeof(void*) > 32) {
34+
ui.only_32bit_1->hide();
35+
ui.only_32bit_2->hide();
36+
}
3337

3438
ui.topLevelStack->setCurrentWidget(ui.mainPage);
3539

OutOfMemoryHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ OutOfMemoryHandler::instance()
3434
// However, because we insist an instance of this object to be created early on,
3535
// the only case that might get us into trouble is an out-of-memory situation
3636
// after main() has returned and this instance got destroyed. This scenario
37-
// sounds rather fantastic, and is not a big deal, as the project would
38-
// already be saved.
37+
// sounds rather fantastic, and is not a big deal, as the project would have
38+
// already been saved.
3939
static OutOfMemoryHandler object;
4040

4141
return object;

ui/OutOfMemoryDialog.ui

Lines changed: 81 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>470</width>
10-
<height>362</height>
10+
<height>342</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -62,6 +62,9 @@ font-weight: bold;</string>
6262
<property name="currentIndex">
6363
<number>0</number>
6464
</property>
65+
<property name="documentMode">
66+
<bool>false</bool>
67+
</property>
6568
<widget class="QWidget" name="tab">
6669
<attribute name="title">
6770
<string>Possible reasons</string>
@@ -89,60 +92,62 @@ font-weight: bold;</string>
8992
<rect>
9093
<x>0</x>
9194
<y>0</y>
92-
<width>448</width>
93-
<height>237</height>
95+
<width>446</width>
96+
<height>227</height>
9497
</rect>
9598
</property>
9699
<layout class="QVBoxLayout" name="verticalLayout_5">
97-
<property name="spacing">
98-
<number>10</number>
99-
</property>
100100
<item>
101101
<widget class="QLabel" name="label_4">
102-
<property name="frameShape">
103-
<enum>QFrame::StyledPanel</enum>
104-
</property>
105-
<property name="frameShadow">
106-
<enum>QFrame::Raised</enum>
107-
</property>
108102
<property name="text">
109103
<string>Did you have to fix the DPI of your source images? Are you sure the values you entered were correct?</string>
110104
</property>
111105
<property name="wordWrap">
112106
<bool>true</bool>
113107
</property>
108+
<property name="openExternalLinks">
109+
<bool>true</bool>
110+
</property>
114111
</widget>
115112
</item>
116113
<item>
117-
<widget class="QLabel" name="label_6">
118-
<property name="frameShape">
119-
<enum>QFrame::StyledPanel</enum>
120-
</property>
121-
<property name="frameShadow">
122-
<enum>QFrame::Raised</enum>
114+
<widget class="Line" name="line">
115+
<property name="orientation">
116+
<enum>Qt::Horizontal</enum>
123117
</property>
118+
</widget>
119+
</item>
120+
<item>
121+
<widget class="QLabel" name="label_6">
124122
<property name="text">
125-
<string>Sometimes your source images may have wrong DPI embedded into them. Scan Tailor tries to detect such cases, but it may miss some. You may need to check &quot;Fix DPI even if they look normal&quot; when creating a project and look into &quot;All pages&quot; tab in the &quot;Fix DPI&quot; dialog.</string>
123+
<string>Sometimes your source images may have wrong DPI embedded into them. Scan Tailor tries to detect those, but it's not always easy to tell. You may need to check &quot;Fix DPI even if they look normal&quot; when creating a project and look into &quot;All pages&quot; tab in the &quot;Fix DPI&quot; dialog, which is also accessible from the Tools menu.</string>
126124
</property>
127125
<property name="wordWrap">
128126
<bool>true</bool>
129127
</property>
128+
<property name="openExternalLinks">
129+
<bool>true</bool>
130+
</property>
130131
</widget>
131132
</item>
132133
<item>
133-
<widget class="QLabel" name="label_8">
134-
<property name="frameShape">
135-
<enum>QFrame::StyledPanel</enum>
136-
</property>
137-
<property name="frameShadow">
138-
<enum>QFrame::Raised</enum>
134+
<widget class="Line" name="line_2">
135+
<property name="orientation">
136+
<enum>Qt::Horizontal</enum>
139137
</property>
138+
</widget>
139+
</item>
140+
<item>
141+
<widget class="QLabel" name="label_8">
140142
<property name="text">
141143
<string>Is your output DPI set too high? Usually you don't need it higher than 600.</string>
142144
</property>
143145
<property name="wordWrap">
144146
<bool>true</bool>
145147
</property>
148+
<property name="openExternalLinks">
149+
<bool>true</bool>
150+
</property>
146151
</widget>
147152
</item>
148153
<item>
@@ -188,77 +193,82 @@ font-weight: bold;</string>
188193
<rect>
189194
<x>0</x>
190195
<y>0</y>
191-
<width>432</width>
192-
<height>330</height>
196+
<width>446</width>
197+
<height>227</height>
193198
</rect>
194199
</property>
195200
<layout class="QVBoxLayout" name="verticalLayout_6">
196-
<property name="spacing">
197-
<number>10</number>
198-
</property>
199201
<item>
200202
<widget class="QLabel" name="label_10">
201-
<property name="frameShape">
202-
<enum>QFrame::StyledPanel</enum>
203-
</property>
204-
<property name="frameShadow">
205-
<enum>QFrame::Raised</enum>
206-
</property>
207203
<property name="text">
208-
<string>Fix your DPIs. Learn how to estimate unknown DPIs.</string>
204+
<string extracomment="Feel free to change the URL to a localized video / text tutorial.">Fix your DPIs. Learn how to &lt;a href=&quot;http://vimeo.com/12524529&quot;&gt;estimate unknown DPIs&lt;/a&gt;.</string>
209205
</property>
210206
<property name="wordWrap">
211207
<bool>true</bool>
212208
</property>
209+
<property name="openExternalLinks">
210+
<bool>true</bool>
211+
</property>
213212
</widget>
214213
</item>
215214
<item>
216-
<widget class="QLabel" name="label_12">
217-
<property name="frameShape">
218-
<enum>QFrame::StyledPanel</enum>
219-
</property>
220-
<property name="frameShadow">
221-
<enum>QFrame::Raised</enum>
215+
<widget class="Line" name="line_3">
216+
<property name="orientation">
217+
<enum>Qt::Horizontal</enum>
222218
</property>
219+
</widget>
220+
</item>
221+
<item>
222+
<widget class="QLabel" name="only_32bit_1">
223223
<property name="text">
224-
<string>When working with grayscale images, make sure they are really grayscale. If they are actually color images just happening to look grayscale, convert them to grayscale using a program like XnView. This will save you significant amount of memory.</string>
224+
<string>If your hardware and operating system are 64-bit capable, consider switching to a 64-bit version of Scan Tailor.</string>
225225
</property>
226226
<property name="wordWrap">
227227
<bool>true</bool>
228228
</property>
229+
<property name="openExternalLinks">
230+
<bool>true</bool>
231+
</property>
229232
</widget>
230233
</item>
231234
<item>
232-
<widget class="QLabel" name="label_14">
233-
<property name="frameShape">
234-
<enum>QFrame::StyledPanel</enum>
235-
</property>
236-
<property name="frameShadow">
237-
<enum>QFrame::Raised</enum>
235+
<widget class="Line" name="only_32bit_2">
236+
<property name="orientation">
237+
<enum>Qt::Horizontal</enum>
238238
</property>
239+
</widget>
240+
</item>
241+
<item>
242+
<widget class="QLabel" name="label_12">
239243
<property name="text">
240-
<string>As a last resort, you can save some memory by making sure thumbnails are pre-created rather than are created on demand. This can be done by slowly scrolling the thumbnail list all the way from top to bottom before starting any work.</string>
244+
<string>When working with grayscale images, make sure they are really grayscale. If they are actually color images that just happen to look grayscale, convert them to grayscale using some kind of batch image converter. This will both save memory and increase performance.</string>
241245
</property>
242246
<property name="wordWrap">
243247
<bool>true</bool>
244248
</property>
249+
<property name="openExternalLinks">
250+
<bool>true</bool>
251+
</property>
245252
</widget>
246253
</item>
247254
<item>
248-
<widget class="QLabel" name="label_18">
249-
<property name="frameShape">
250-
<enum>QFrame::StyledPanel</enum>
251-
</property>
252-
<property name="frameShadow">
253-
<enum>QFrame::Raised</enum>
255+
<widget class="Line" name="line_5">
256+
<property name="orientation">
257+
<enum>Qt::Horizontal</enum>
254258
</property>
259+
</widget>
260+
</item>
261+
<item>
262+
<widget class="QLabel" name="label_14">
255263
<property name="text">
256-
<string>Moving from a 32-bit to 64-bit version of Windows will increase limit of available memory for 32-bit applications from 2 to 3 GB.
257-
[For experts only!] There is a way to achieve the same effect on 32-bit versions of Windows.</string>
264+
<string>As a last resort, you can save some memory by making sure thumbnails are pre-created rather than created on demand. This can be done by slowly scrolling the thumbnail list all the way from top to bottom before starting any real work.</string>
258265
</property>
259266
<property name="wordWrap">
260267
<bool>true</bool>
261268
</property>
269+
<property name="openExternalLinks">
270+
<bool>true</bool>
271+
</property>
262272
</widget>
263273
</item>
264274
<item>
@@ -284,10 +294,7 @@ font-weight: bold;</string>
284294
<attribute name="title">
285295
<string>What won't help</string>
286296
</attribute>
287-
<layout class="QVBoxLayout" name="verticalLayout_7">
288-
<property name="spacing">
289-
<number>0</number>
290-
</property>
297+
<layout class="QVBoxLayout" name="verticalLayout_8">
291298
<property name="margin">
292299
<number>0</number>
293300
</property>
@@ -296,6 +303,9 @@ font-weight: bold;</string>
296303
<property name="frameShape">
297304
<enum>QFrame::NoFrame</enum>
298305
</property>
306+
<property name="frameShadow">
307+
<enum>QFrame::Plain</enum>
308+
</property>
299309
<property name="widgetResizable">
300310
<bool>true</bool>
301311
</property>
@@ -304,28 +314,22 @@ font-weight: bold;</string>
304314
<rect>
305315
<x>0</x>
306316
<y>0</y>
307-
<width>448</width>
308-
<height>237</height>
317+
<width>446</width>
318+
<height>227</height>
309319
</rect>
310320
</property>
311-
<layout class="QVBoxLayout" name="verticalLayout_8">
312-
<property name="spacing">
313-
<number>10</number>
314-
</property>
321+
<layout class="QVBoxLayout" name="verticalLayout_7">
315322
<item>
316-
<widget class="QLabel" name="label_16">
317-
<property name="frameShape">
318-
<enum>QFrame::StyledPanel</enum>
319-
</property>
320-
<property name="frameShadow">
321-
<enum>QFrame::Raised</enum>
322-
</property>
323+
<widget class="QLabel" name="label_5">
323324
<property name="text">
324-
<string>Upgrading your RAM or any other hardware won't help. The operating system uses swapping to compensate the lack of RAM. However, 32-bit programs are limited to 3 (often 2) GB of memory. Scan Tailor just hit that limit.</string>
325+
<string>Surprisingly, upgrading your RAM won't help here. The lack of RAM is compensated by the swap mechanism, which makes things slow, but keeps programs running. An out of memory situation means we ran out of memory address space, which has nothing to do with the amount of RAM you have. The only way to increase the memory address space is to go 64-bit hardware, 64-bit operating system and 64-bit Scan Tailor.</string>
325326
</property>
326327
<property name="wordWrap">
327328
<bool>true</bool>
328329
</property>
330+
<property name="openExternalLinks">
331+
<bool>true</bool>
332+
</property>
329333
</widget>
330334
</item>
331335
<item>

0 commit comments

Comments
 (0)