]> git.lyx.org Git - features.git/commitdiff
* Do not keep pointers to data structures around if you don't know
authorStefan Schimanski <sts@lyx.org>
Tue, 5 Feb 2008 10:34:01 +0000 (10:34 +0000)
committerStefan Schimanski <sts@lyx.org>
Tue, 5 Feb 2008 10:34:01 +0000 (10:34 +0000)
  whether the data structure outlives the pointer:

  InsetLayouts are really owned by the corresponding TextClass. So
  keep the TextClass alive by keeping a TextClassPtr around for the
  pointers lifetime. This fixes Bug #4538.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22782 a592a061-630c-0410-9148-cb99ea01b6c8

src/CutAndPaste.cpp
src/factory.cpp
src/insets/InsetCollapsable.cpp
src/insets/InsetCollapsable.h
src/insets/InsetFlex.cpp
src/insets/InsetFlex.h

index f657d57cbde9d0a876bc52c623142cb96964902f..63f25267adbe5bfccf098a4a3aead7c4c1d413ee 100644 (file)
@@ -445,17 +445,15 @@ void switchBetweenClasses(TextClassPtr const & c1,
                if (inset->lyxCode() != FLEX_CODE)
                        // FIXME: Should we verify all InsetCollapsable?
                        continue;
-               docstring const name = inset->name();
-               InsetLayout const & il = tclass2.insetlayout(name);
-               inset->setLayout(il);
-               if (il.labelstring != from_utf8("UNDEFINED"))
+               inset->setLayout(c2);
+               if (inset->getLayout().labelstring != from_utf8("UNDEFINED"))
                        continue;
                // The flex inset is undefined in tclass2
                docstring const s = bformat(_(
                        "Flex inset %1$s is "
                        "undefined because of class "
                        "conversion from\n%2$s to %3$s"),
-                       name, from_utf8(tclass1.name()),
+                       inset->name(), from_utf8(tclass1.name()),
                        from_utf8(tclass2.name()));
                // To warn the user that something had to be done.
                errorlist.push_back(ErrorItem(
index d3b29e2f22e83b14c083c2ad8114e0101a49f5e6..fd823085ee6e6b194dbc2f95ca3b05585224946e 100644 (file)
@@ -102,9 +102,7 @@ Inset * createInset(Buffer & buf, FuncRequest const & cmd)
 
                case LFUN_FLEX_INSERT: {
                        string s = cmd.getArg(0);
-                       TextClass const & tclass = params.getTextClass();
-                       InsetLayout const & il = tclass.insetlayout(from_utf8(s));
-                       return new InsetFlex(params, il);
+                       return new InsetFlex(params, params.getTextClassPtr(), s);
                }
 
                case LFUN_NOTE_INSERT: {
@@ -385,8 +383,6 @@ Inset * readInset(Lexer & lex, Buffer const & buf)
 
        auto_ptr<Inset> inset;
 
-       TextClass const & tclass = buf.params().getTextClass();
-
        lex.next();
        string tmptok = lex.getString();
 
@@ -480,8 +476,8 @@ Inset * readInset(Lexer & lex, Buffer const & buf)
                } else if (tmptok == "Flex") {
                        lex.next();
                        string s = lex.getString();
-                       InsetLayout const & il = tclass.insetlayout(from_utf8(s));
-                       inset.reset(new InsetFlex(buf.params(), il));
+                       inset.reset(new InsetFlex(buf.params(), 
+                               buf.params().getTextClassPtr(), s));
                } else if (tmptok == "Branch") {
                        inset.reset(new InsetBranch(buf.params(),
                                                    InsetBranchParams()));
index aaea6264ebaaedece3bda88c6c67a76af59dae0c..95243082d8e827cbeec704ccb7375d3cd4f8b04c 100644 (file)
@@ -74,10 +74,11 @@ InsetCollapsable::Geometry InsetCollapsable::geometry() const
 
 
 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
-               CollapseStatus status, InsetLayout const * il)
-       : InsetText(bp), layout_(il), status_(status),
+               CollapseStatus status, TextClassPtr tc)
+       : InsetText(bp), status_(status),
          openinlined_(false), autoOpen_(false), mouse_hover_(false)
 {
+       setLayout(tc);
        setAutoBreakRows(true);
        setDrawFrame(true);
        setFrameColor(Color_collapsableframe);
@@ -86,6 +87,7 @@ InsetCollapsable::InsetCollapsable(BufferParams const & bp,
 
 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
        : InsetText(rhs),
+               textClass_(rhs.textClass_),
                layout_(rhs.layout_),
                labelstring_(rhs.labelstring_),
                button_dim(rhs.button_dim),
@@ -116,14 +118,20 @@ docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
 
 void InsetCollapsable::setLayout(BufferParams const & bp)
 {
-       setLayout(bp.getTextClass().insetlayout(name()));
+       setLayout(bp.getTextClassPtr());
 }
 
 
-void InsetCollapsable::setLayout(InsetLayout const & il)
+void InsetCollapsable::setLayout(TextClassPtr tc)
 {
-       layout_ = &il;
-       labelstring_ = layout_->labelstring;
+       textClass_ = tc;
+       if ( tc.get() != 0 ) {
+               layout_ = &tc->insetlayout(name());
+               labelstring_ = layout_->labelstring;
+       } else {
+               layout_ = 0;
+               labelstring_ = _("UNDEFINED");
+       }
 
        setButtonLabel();
 }
index 6c271eceaed3a2c7a3b3927bb535dd2ef94f7b0c..c507cda475f0030d862d9c877b6be800982ad826 100644 (file)
@@ -19,6 +19,8 @@
 #include "InsetText.h"
 
 #include "Box.h"
+#include "TextClass.h"
+#include "TextClassPtr.h"
 
 #include <string>
 
@@ -41,7 +43,7 @@ public:
        InsetCollapsable(
                BufferParams const &,
                CollapseStatus status = Inset::Open,
-               InsetLayout const * il = 0
+               TextClassPtr tc = TextClassPtr((TextClass *)0)
                );
        ///
        InsetCollapsable(InsetCollapsable const & rhs);
@@ -52,10 +54,13 @@ public:
        docstring name() const { return from_ascii("Collapsable"); }
        InsetLayout const & getLayout(BufferParams const &) const
        { return *layout_; } 
+       InsetLayout const & getLayout() const
+       { return *layout_; } 
        ///
        void setLayout(BufferParams const &);
-       /// (Re-)set the character style parameters from \p il
-       void setLayout(InsetLayout const & il);
+       /// (Re-)set the character style parameters from \p tc according
+       /// to name()
+       void setLayout(TextClassPtr tc);
        ///
        void read(Buffer const &, Lexer &);
        ///
@@ -165,10 +170,11 @@ protected:
        ///
        virtual void resetParagraphsFont();
 
-protected:
-       ///
-       InsetLayout const * layout_;
 private:
+       /// text class to keep the InsetLayout above in memory
+       TextClassPtr textClass_;
+       /// cache for the layout_. Make sure it is in sync with the text class!
+       InsetLayout const * layout_;
        ///
        Dimension dimensionCollapsed() const;
        ///
index 50c16a50cd33208da615ca673f8ad22da243ba94..9555cb2461a1bc69bdd18430af5741152095cd01 100644 (file)
@@ -43,12 +43,13 @@ namespace lyx {
 
 
 InsetFlex::InsetFlex(BufferParams const & bp,
-                               InsetLayout const & il)
-       : InsetCollapsable(bp, Collapsed, &il)
+       TextClassPtr tc, string const & layoutName)
+       : InsetCollapsable(bp, Collapsed, tc),
+       name_(layoutName)
 {
-       name_ = il.name;
-       packages_ = il.requires;
-       preamble_ = il.preamble;
+       setLayout(tc); // again, because now the name is initialized
+       packages_ = getLayout().requires;
+       preamble_ = getLayout().preamble;
 }
 
 
@@ -65,7 +66,7 @@ Inset * InsetFlex::clone() const
 
 bool InsetFlex::undefined() const
 {
-       return layout_->labelstring == from_utf8("UNDEFINED");
+       return getLayout().labelstring == from_utf8("UNDEFINED");
 }
 
 
@@ -118,8 +119,8 @@ int InsetFlex::docbook(Buffer const & buf, odocstream & os,
        ParagraphList::const_iterator end = paragraphs().end();
 
        if (!undefined())
-               sgml::openTag(os, layout_->latexname,
-                             par->getID(buf, runparams) + layout_->latexparam);
+               sgml::openTag(os, getLayout().latexname,
+                             par->getID(buf, runparams) + getLayout().latexparam);
 
        for (; par != end; ++par) {
                par->simpleDocBookOnePar(buf, os, runparams,
@@ -128,7 +129,7 @@ int InsetFlex::docbook(Buffer const & buf, odocstream & os,
        }
 
        if (!undefined())
-               sgml::closeTag(os, layout_->latexname);
+               sgml::closeTag(os, getLayout().latexname);
 
        return 0;
 }
index 141beb1b8076765f68d49764d8deb94a3a469362..7ea771bd9c19b3543646a2f4295e155f85c1c9ae 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "InsetCollapsable.h"
 
+using std::string;
 
 namespace lyx {
 
@@ -25,7 +26,8 @@ namespace lyx {
 class InsetFlex : public InsetCollapsable {
 public:
        ///
-       InsetFlex(BufferParams const &, InsetLayout const &);
+       InsetFlex(BufferParams const &,
+                 TextClassPtr tc, string const & layoutName);
        ///
        docstring name() const { return from_utf8(name_); }