]> git.lyx.org Git - features.git/blobdiff - src/mathed/MathMacro.cpp
Avoid using a dangling pointer
[features.git] / src / mathed / MathMacro.cpp
index 78ea2bb3654913789464948ac5efeb8a89684fec..e772bc1b04eec024a6dceee9e0f2a2f950971af5 100644 (file)
@@ -22,6 +22,7 @@
 #include "MathSupport.h"
 
 #include "Buffer.h"
+#include "BufferList.h"
 #include "BufferView.h"
 #include "CoordCache.h"
 #include "Cursor.h"
@@ -207,7 +208,10 @@ MathMacro::MathMacro(MathMacro const & that)
                // We need to update d->macro_ by ourselves because in this case
                // MathData::metrics() is not called when selecting a math inset
                DocIterator const & pos = d->macroBackup_.pos();
-               d->macro_ = pos.buffer() ? pos.buffer()->getMacro(name(), pos) : 0;
+               Buffer const * buf = pos.buffer();
+               if (buf && !theBufferList().isLoaded(buf))
+                       buf = 0;
+               d->macro_ = buf ? buf->getMacro(name(), pos) : 0;
                if (!d->macro_)
                        d->macro_ = &d->macroBackup_;
        }
@@ -225,7 +229,10 @@ MathMacro & MathMacro::operator=(MathMacro const & that)
                // We need to update d->macro_ by ourselves because in this case
                // MathData::metrics() is not called when selecting a math inset
                DocIterator const & pos = d->macroBackup_.pos();
-               d->macro_ = pos.buffer() ? pos.buffer()->getMacro(name(), pos) : 0;
+               Buffer const * buf = pos.buffer();
+               if (buf && !theBufferList().isLoaded(buf))
+                       buf = 0;
+               d->macro_ = buf ? buf->getMacro(name(), pos) : 0;
                if (!d->macro_)
                        d->macro_ = &d->macroBackup_;
        }