diff --git a/src/wrapper/wrap_cl.hpp b/src/wrapper/wrap_cl.hpp
index 2356d7df291d749860911087d9afdc837ef90d58..88dcec9255148e73a89d38c8b20f74b164345e2f 100644
--- a/src/wrapper/wrap_cl.hpp
+++ b/src/wrapper/wrap_cl.hpp
@@ -780,7 +780,7 @@ namespace pyopencl
 
 
   inline
-  context *create_context(py::object py_devices, py::object py_properties,
+  context *create_context_inner(py::object py_devices, py::object py_properties,
       py::object py_dev_type)
   {
     std::vector<cl_context_properties> props
@@ -844,6 +844,33 @@ namespace pyopencl
 
 
 
+  inline
+  context *create_context(py::object py_devices, py::object py_properties,
+      py::object py_dev_type)
+  {
+    try
+    {
+      return create_context_inner(py_devices, py_properties, py_dev_type);
+    }
+    catch (pyopencl::error &e)
+    { 
+      if (!e.is_out_of_memory())
+        throw;
+    }
+
+    // If we get here, we got an error from CL.
+    // We should run the Python GC to try and free up
+    // some memory references.
+    run_python_gc();
+
+    // Now retry the allocation. If it fails again,
+    // let it fail.
+    return create_context_inner(py_devices, py_properties, py_dev_type);
+  }
+
+
+
+
 
   // }}}