diff --git a/python/core/__init__.py b/python/core/__init__.py
index 07123520f39..9996cd7bea4 100644
--- a/python/core/__init__.py
+++ b/python/core/__init__.py
@@ -210,6 +210,8 @@ class edit(object):
             self.layer.rollBack()
             return False
 
+# Python class to mimic QgsReadWriteContextCategoryPopper C++ class
+
 
 class ReadWriteContextEnterCategory():
     def __init__(self, context, category_name, details=None):
@@ -227,6 +229,7 @@ class ReadWriteContextEnterCategory():
         return True
 
 
+# Inject the context manager into QgsReadWriteContext class as a member
 QgsReadWriteContext.enterCategory = ReadWriteContextEnterCategory
 
 
diff --git a/python/core/qgsreadwritecontext.sip.in b/python/core/qgsreadwritecontext.sip.in
index d33b16ba38f..66fb4f69193 100644
--- a/python/core/qgsreadwritecontext.sip.in
+++ b/python/core/qgsreadwritecontext.sip.in
@@ -116,7 +116,7 @@ This would happen when it gets out of scope.
 #include "qgsreadwritecontext.h"
 %End
   public:
-    QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context );
+    QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context );
 %Docstring
 Creates a popper
 %End
diff --git a/src/core/qgsreadwritecontext.cpp b/src/core/qgsreadwritecontext.cpp
index 4baa772c16e..cdda338a065 100644
--- a/src/core/qgsreadwritecontext.cpp
+++ b/src/core/qgsreadwritecontext.cpp
@@ -41,7 +41,7 @@ QgsReadWriteContextCategoryPopper QgsReadWriteContext::enterCategory( const QStr
   if ( !details.isEmpty() )
     message.append( QString( " :: %1" ).arg( details ) );
   mCategories.push_back( message );
-  return QgsReadWriteContextCategoryPopper( *this );
+  return QgsReadWriteContextCategoryPopper( this );
 }
 
 void QgsReadWriteContext::leaveCategory()
diff --git a/src/core/qgsreadwritecontext.h b/src/core/qgsreadwritecontext.h
index e97a51d6ef6..5f32e9e13ca 100644
--- a/src/core/qgsreadwritecontext.h
+++ b/src/core/qgsreadwritecontext.h
@@ -122,10 +122,14 @@ class CORE_EXPORT QgsReadWriteContextCategoryPopper
 {
   public:
     //! Creates a popper
-    QgsReadWriteContextCategoryPopper( QgsReadWriteContext &context ) : mContext( context ) {}
-    ~QgsReadWriteContextCategoryPopper() {mContext.leaveCategory();}
+    QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context ) : mContext( context ) {}
+    ~QgsReadWriteContextCategoryPopper()
+    {
+      if ( mContext )
+        mContext->leaveCategory();
+    }
   private:
-    QgsReadWriteContext mContext;
+    QgsReadWriteContext *mContext;
 };
 
 #endif // QGSREADWRITECONTEXT_H