diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2016-04-20 17:15:11 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2016-04-20 17:19:49 +0300 |
commit | 02213b867ed68a00683fa931ebce12a23ed3e77b (patch) | |
tree | 3cf74a5799da5f7106b4a2d2aa0eca23b9a56747 /intern | |
parent | e3544c9e28eded2403613d2bc5af8992c3aa734a (diff) |
Cycles: Stop rendering when bad_alloc happens
This is an attempt to gracefully handle out-of-memory events
and stop rendering with an error message instead of a crash.
It uses bad_alloc exception, and usually i'm not really fond
of exceptions, but for such limited use for errors from which
we can't recover it should be fine.
Ideally we'll need to stop full Cycles Session, so viewport
render and persistent images frees all the memory, but that
we can support later, since it'll mainly related on telling
Blender what to do.
General rules are:
- Use as less exception handles as possible, try to find a
most geenric pace where to handle those.
For example, ccl::Session.
- Threads needs own handling, exception trap from one thread
will not catch exceptions from other threads.
That's why BVH build needs own thing.
Reviewers: brecht, juicyfruit, dingto, lukasstockner97
Differential Revision: https://developer.blender.org/D1898
Diffstat (limited to 'intern')
-rw-r--r-- | intern/cycles/render/mesh.cpp | 2 | ||||
-rw-r--r-- | intern/cycles/render/session.cpp | 2 | ||||
-rw-r--r-- | intern/cycles/util/util_guarded_allocator.h | 20 |
3 files changed, 22 insertions, 2 deletions
diff --git a/intern/cycles/render/mesh.cpp b/intern/cycles/render/mesh.cpp index 241a1c44ebf..990f725a5e3 100644 --- a/intern/cycles/render/mesh.cpp +++ b/intern/cycles/render/mesh.cpp @@ -528,7 +528,7 @@ void Mesh::compute_bvh(SceneParams *params, Progress *progress, int n, int total delete bvh; bvh = BVH::create(bparams, objects); - bvh->build(*progress); + MEM_GUARDED_CALL(progress, bvh->build, *progress); } } diff --git a/intern/cycles/render/session.cpp b/intern/cycles/render/session.cpp index 24f48b61349..63037311889 100644 --- a/intern/cycles/render/session.cpp +++ b/intern/cycles/render/session.cpp @@ -816,7 +816,7 @@ void Session::update_scene() /* update scene */ if(scene->need_update()) { progress.set_status("Updating Scene"); - scene->device_update(device, progress); + MEM_GUARDED_CALL(&progress, scene->device_update, device, progress); } } diff --git a/intern/cycles/util/util_guarded_allocator.h b/intern/cycles/util/util_guarded_allocator.h index 3e63588e9b6..78453d214be 100644 --- a/intern/cycles/util/util_guarded_allocator.h +++ b/intern/cycles/util/util_guarded_allocator.h @@ -164,6 +164,26 @@ public: size_t util_guarded_get_mem_used(void); size_t util_guarded_get_mem_peak(void); +/* Call given function and keep track if it runs out of memory. + * + * If it does run out f memory, stop execution and set progress + * to do a global cancel. + * + * It's not fully robust, but good enough to catch obvious issues + * when running out of memory. + */ +#define MEM_GUARDED_CALL(progress, func, ...) \ + do { \ + try { \ + (func)(__VA_ARGS__); \ + } \ + catch (std::bad_alloc&) { \ + fprintf(stderr, "Error: run out of memory!\n"); \ + fflush(stderr); \ + (progress)->set_error("Out of memory"); \ + } \ + } while(false) + CCL_NAMESPACE_END #endif /* __UTIL_GUARDED_ALLOCATOR_H__ */ |