Author: rgheck
Date: Thu Nov 17 19:06:06 2011
New Revision: 40206
URL: http://www.lyx.org/trac/changeset/40206

Log:
Backport fix for crash reported by Pavel.

Modified:
   lyx-devel/branches/BRANCH_2_0_X/src/Buffer.cpp

Modified: lyx-devel/branches/BRANCH_2_0_X/src/Buffer.cpp
==============================================================================
--- lyx-devel/branches/BRANCH_2_0_X/src/Buffer.cpp      Thu Nov 17 18:58:22 
2011        (r40205)
+++ lyx-devel/branches/BRANCH_2_0_X/src/Buffer.cpp      Thu Nov 17 19:06:06 
2011        (r40206)
@@ -140,6 +140,10 @@
        Alert::error(_("Print document failed"), str);
 }
 
+/// a list of Buffers we cloned
+set<Buffer *> cloned_buffer_list;
+
+
 } // namespace anon
 
 
@@ -386,36 +390,50 @@
                return;
        }
 
-       // loop over children
-       Impl::BufferPositionMap::iterator it = d->children_positions.begin();
-       Impl::BufferPositionMap::iterator end = d->children_positions.end();
-       for (; it != end; ++it) {
-               Buffer * child = const_cast<Buffer *>(it->first);
-               if (d->cloned_buffer_)
-                       delete child;
-               // The child buffer might have been closed already.
-               else if (theBufferList().isLoaded(child))
-                       theBufferList().releaseChild(this, child);
-       }
-
-       if (!isClean()) {
-               docstring msg = _("LyX attempted to close a document that had 
unsaved changes!\n");
-               msg += emergencyWrite();
-               Alert::warning(_("Attempting to close changed document!"), msg);
-       }
-               
-       // clear references to children in macro tables
-       d->children_positions.clear();
-       d->position_to_children.clear();
-
-       if (!d->cloned_buffer_ && !d->temppath.destroyDirectory()) {
-               Alert::warning(_("Could not remove temporary directory"),
-                       bformat(_("Could not remove the temporary directory 
%1$s"),
-                       from_utf8(d->temppath.absFileName())));
-       }
-
-       if (!isClone())
+       if (isClone()) {
+               // this is in case of recursive includes: we won't try to delete
+               // ourselves as a child.
+               cloned_buffer_list.erase(this);
+               // loop over children
+               Impl::BufferPositionMap::iterator it = 
d->children_positions.begin();
+               Impl::BufferPositionMap::iterator end = 
d->children_positions.end();
+               for (; it != end; ++it) {
+                       Buffer * child = const_cast<Buffer *>(it->first);
+                               if (cloned_buffer_list.erase(child))
+                                       delete child;
+               }
+               // FIXME Do we really need to do this right before we delete d?
+               // clear references to children in macro tables
+               d->children_positions.clear();
+               d->position_to_children.clear();
+       } else {
+               // loop over children
+               Impl::BufferPositionMap::iterator it = 
d->children_positions.begin();
+               Impl::BufferPositionMap::iterator end = 
d->children_positions.end();
+               for (; it != end; ++it) {
+                       Buffer * child = const_cast<Buffer *>(it->first);
+                       if (theBufferList().isLoaded(child))
+                               theBufferList().releaseChild(this, child);
+               }
+
+               if (!isClean()) {
+                       docstring msg = _("LyX attempted to close a document 
that had unsaved changes!\n");
+                       msg += emergencyWrite();
+                       Alert::warning(_("Attempting to close changed 
document!"), msg);
+               }
+
+               // FIXME Do we really need to do this right before we delete d?
+               // clear references to children in macro tables
+               d->children_positions.clear();
+               d->position_to_children.clear();
+
+               if (!d->temppath.destroyDirectory()) {
+                       Alert::warning(_("Could not remove temporary 
directory"),
+                               bformat(_("Could not remove the temporary 
directory %1$s"),
+                               from_utf8(d->temppath.absFileName())));
+               }
                removePreviews();
+       }
 
        delete d;
 }
@@ -425,9 +443,20 @@
 {
        BufferMap bufmap;
        masterBuffer()->clone(bufmap);
-       BufferMap::iterator it = bufmap.find(this);
-       LASSERT(it != bufmap.end(), return 0);
-       return it->second;
+
+       // make sure we got cloned
+       BufferMap::const_iterator bit = bufmap.find(this);
+       LASSERT(bit != bufmap.end(), return 0);
+       Buffer * cloned_buffer = bit->second;
+
+       // record the list of cloned buffers
+       cloned_buffer_list.clear();
+       BufferMap::iterator it = bufmap.begin();
+       BufferMap::iterator en = bufmap.end();
+       for (; it != en; ++it)
+               cloned_buffer_list.insert(it->second);
+
+       return cloned_buffer;
 }
 
 

Reply via email to