[Libreoffice-commits] core.git: Branch 'libreoffice-5-0' - lotuswordpro/source

Caolán McNamara caolanm at redhat.com
Mon Dec 14 06:01:59 PST 2015


 lotuswordpro/source/filter/lwpcelllayout.cxx          |   25 -
 lotuswordpro/source/filter/lwpcontent.cxx             |   20 
 lotuswordpro/source/filter/lwpcontent.hxx             |    2 
 lotuswordpro/source/filter/lwpdivinfo.cxx             |    4 
 lotuswordpro/source/filter/lwpdoc.cxx                 |   72 +--
 lotuswordpro/source/filter/lwpdoc.hxx                 |   23 -
 lotuswordpro/source/filter/lwpfnlayout.cxx            |   12 
 lotuswordpro/source/filter/lwpfootnote.cxx            |    6 
 lotuswordpro/source/filter/lwpfoundry.cxx             |   42 -
 lotuswordpro/source/filter/lwpframelayout.cxx         |  104 ++--
 lotuswordpro/source/filter/lwpfrib.cxx                |    2 
 lotuswordpro/source/filter/lwpfribbreaks.cxx          |    9 
 lotuswordpro/source/filter/lwpfribframe.cxx           |    6 
 lotuswordpro/source/filter/lwpfribsection.cxx         |   43 -
 lotuswordpro/source/filter/lwpfribtable.cxx           |   12 
 lotuswordpro/source/filter/lwpgrfobj.cxx              |    6 
 lotuswordpro/source/filter/lwplayout.cxx              |  413 +++++++++++-------
 lotuswordpro/source/filter/lwplayout.hxx              |   51 +-
 lotuswordpro/source/filter/lwpnotes.cxx               |    6 
 lotuswordpro/source/filter/lwpobjfactory.cxx          |   14 
 lotuswordpro/source/filter/lwpobjfactory.hxx          |    3 
 lotuswordpro/source/filter/lwpoleobject.cxx           |    6 
 lotuswordpro/source/filter/lwppagelayout.cxx          |   32 -
 lotuswordpro/source/filter/lwppara.cxx                |   24 -
 lotuswordpro/source/filter/lwppara1.cxx               |    8 
 lotuswordpro/source/filter/lwpparaproperty.cxx        |    4 
 lotuswordpro/source/filter/lwpparastyle.cxx           |    3 
 lotuswordpro/source/filter/lwpproplist.cxx            |   11 
 lotuswordpro/source/filter/lwpsdwgrouploaderv0102.cxx |   16 
 lotuswordpro/source/filter/lwpstory.cxx               |  152 ++----
 lotuswordpro/source/filter/lwpstory.hxx               |    2 
 lotuswordpro/source/filter/lwptable.hxx               |    5 
 lotuswordpro/source/filter/lwptablelayout.cxx         |  113 ++--
 lotuswordpro/source/filter/lwptblcell.cxx             |    2 
 lotuswordpro/source/filter/lwptblcell.hxx             |    2 
 lotuswordpro/source/filter/lwptblformula.cxx          |   19 
 lotuswordpro/source/filter/lwptoc.cxx                 |   23 -
 lotuswordpro/source/filter/tocread.cxx                |    2 
 lotuswordpro/source/filter/xfilter/xfcell.cxx         |    4 
 lotuswordpro/source/filter/xfilter/xftable.cxx        |   12 
 lotuswordpro/source/filter/xfilter/xftable.hxx        |    2 
 41 files changed, 754 insertions(+), 563 deletions(-)

New commits:
commit d1d9e61de393aaa8e08edf8f96f134ba1eea9578
Author: Caolán McNamara <caolanm at redhat.com>
Date:   Thu Dec 10 12:00:52 2015 +0000

    lwp cycle 1 fixes
    
    guard against missing Foundry
    
    (cherry picked from commit 92c15e8b299504ef76847cceb5911ef6c7ff26f1)
    
    guard against missing grandfather layout
    
    (cherry picked from commit f9164c1785432996092d376fb3983e1a3f78dad7)
    
    guard against corrupt LwpTocSuperLayout
    
    (cherry picked from commit e0dca588239c0902ea90fcdc2b6d0ee2b1525ec2)
    
    don't trust that this is what its supposed to be
    
    (cherry picked from commit f792baefbfb645eafd5e6b7073bfe6e389d666dc)
    
    guard against missing HeadHolder
    
    (cherry picked from commit ca868de5e41b45a077949e40ef80f063c362892b)
    
    avoid divide by zero
    
    (cherry picked from commit 92b936e3ef947cc096b1445455332625900c3024)
    
    guard against missing Foundry
    
    (cherry picked from commit ed09d4f55d752dbc7d815fdc90e6cbe2656690b7)
    
    check is within legal bounds
    
    (cherry picked from commit 8410527550ef6db8348e8590eb0c52bccbe40310)
    
    don't trust that this is what its supposed to be
    
    (cherry picked from commit 9ad6f07e0b5494e55d70642120f173961845ce30)
    
    check dynamic casts
    
    (cherry picked from commit 54e95789adf05c6852db18b80a549b789a40d6d3)
    
    guard against missing Foundry
    
    (cherry picked from commit 6093ba6065ea339c5c6de162294eabf0eebe3c80)
    
    guard against missing range
    
    (cherry picked from commit 1aff042d88b2d6d3c3b7ed9db4b36bed640a0f06)
    
    there can be 0 columns
    
    (cherry picked from commit 6857a386a4cfe4ddb42515cb00df5bd6648d0964)
    
    need dynamic casts here, etc.
    
    (cherry picked from commit 46fe3bddebf30775ae19eaa0fefe1d8e2f78eced)
    
    add a reference to ensure pPara exists until end of scope
    
    (cherry picked from commit 61f55b25c62eaa2714ad8b991edcf54be5e41e0c)
    
    this needs to be a dynamic cast
    
    (cherry picked from commit 67296edb5c8c7cbb185fe2d31c46eadc5dd927b8)
    
    detect infinite recurse in object creation
    
    (cherry picked from commit 48c3eb3c91fd98c313bcec18f24cc949d6e788b3)
    
    check for null content
    
    (cherry picked from commit 4a573e67c67ddf15403a79e7ec8d984d189dc83a)
    
    check for null Modifiers
    
    (cherry picked from commit 6834384e80cf5cd374cdec471ce385ba2ca94580)
    
    guard against broken formula and bad lengths
    
    (cherry picked from commit 1e0b9881b4dc7d002d5da767f1476fb1c2147fc7)
    
    guard against empty Story
    
    (cherry picked from commit ef68438d384ed6dd919fa0f1a3830d2171f8e002)
    
    need a dynamic cast here
    
    53rd and final distinct crash fix of this sequence
    
    (cherry picked from commit 3735b05668ffcaa37fcecf2907478616db52f2b4)
    
    coverity#1341875 Division or modulo by float zero
    
    (cherry picked from commit 18952ac1de81b522f5fef36ff922c4a0dc29499d)
    
    guard against missing m_pXFContainer
    
    (cherry picked from commit 13eaf4dad1324b4f2d909424b2bf06f75102f01f)
    
    guard against missing table
    
    (cherry picked from commit 0034019480167d50df33865389a6d21e8d5b9cec)
    
    guard against empty XFDrawObjects
    
    (cherry picked from commit e9801b286b7af0a6043a3a70090a49c5959abe16)
    
    this cast needs to be dynamic
    
    (cherry picked from commit 8d57f2fb953f031c48892366ce52f6d4fcb9c025)
    
    this cast needs to be dynamic
    
    (cherry picked from commit 25632b8ffe53dd18faaab2f5c019f7a401ebf579)
    
    check all obj().get() casts with dynamic_cast
    
    (cherry picked from commit 411dc2058d222a5c2f9b8b159b9384086c9219a5)
    
    guard against missing Container Layout
    
    (cherry picked from commit dd43c614980ed330936df1abcc484452e1c71a09)
    
    guard against null pContent
    
    (cherry picked from commit c910e229aa19edd0502aa05dffedc91dd727da6f)
    
    guard against infinite recursion
    
    (cherry picked from commit 5df326438fd3a5613a52b4de1935426911ff1301)
    
    cppcheck: fix memleak in lotuswordpro
    
    (cherry picked from commit 6e1efd486dfd5a4bcff1405158c9e40e0c2b6524)
    
    infinite recurse protection
    
    (cherry picked from commit fb8cba16caa87bf21fdd2747b22d90d913557dac)
    
    need more dynamic casts here
    
    (cherry picked from commit 5d8449cd14aa3d08929bb25626ab54aa7515f47d)
    
    recursion protection
    
    (cherry picked from commit 4e6df6de1a95b6b4194cf930cabaae7fde3b6960)
    
    protect against missing Foundry
    
    (cherry picked from commit ddc7d96a399822c2f29dcaca8222dfb96c383576)
    
    guard against missing header rows
    
    (cherry picked from commit 2487ba80581d8b0f50be40a26255789e1a050e95)
    
    deploy references to keep layout alive long enough
    
    (cherry picked from commit 1deae8a2d92747ad69b024513ddae93cc8927d29)
    
    use more references to fix life cycles
    
    (cherry picked from commit 62a6cadbab6c7bdfbd605c1c75c250904ed10442)
    
    use more references to fix life cycles
    
    (cherry picked from commit a1426bc54b11eb48fdefaf7e8dfd7996e5c7e4d5)
    
    deploy references to keep layout alive long enough
    
    (cherry picked from commit fdf122378d5dfa7160fa29eace026424b125e3e4)
    
    use more references to fix life cycles
    
    (cherry picked from commit 0876868a14c7d139ed2a79ece989c84239a2c853)
    
    protect both EnumNamedProperty branches the same way
    
    (cherry picked from commit a065eda2383e8d2d8de3cf0a280f6699c05da425)
    
    guard against no container layout
    
    (cherry picked from commit c6a2df4061de028eb9d7fb752051ecb9f1621fef)
    
    guard against invalid Layout Scale
    
    (cherry picked from commit c66ed9591217a612c7adc22757ea2b760a716ee3)
    
    use more references to fix life cycles
    
    (cherry picked from commit a258c3a6503b4fd76ad2b0f705fdd7f472f58c4b)
    
    use more references to fix life cycles
    
    (cherry picked from commit a23f7b3c14ad0e88264aca197a979aefcc9c0670)
    
    use more references to fix life cycles
    
    (cherry picked from commit 64cfe976c36cb930e0a96557ba7d9eb99442fc59)
    
    hold WaterMarkLayout by reference
    
    (cherry picked from commit 80509545cc43893776dd24f441e9b95567ce06db)
    
    most casts and checks
    
    (cherry picked from commit 01f920f2cece7a05ea075130c19229029efe7c0f)
    
    font might be null
    
    (cherry picked from commit 00bccc565d1bcd4bd620bb9b33b9f1d07fb5f13e)
    
    more use of references to avoid crashing
    
    (cherry picked from commit 69d166a0277d21d63b97ffc965d82bb301d55502)
    
    more lifecycle protection
    
    (cherry picked from commit 012bdecfcd7094403516c98bd5231fe1ef6f7e9d)
    
    guard against infinite recursion
    
    (cherry picked from commit 76220aafe0bffe20f10b07c725cd096adb56b108)
    
    need more References for life cycle fixes here
    
    32nd and final distinct crash fix of this sequence
    
    (cherry picked from commit 9550737e5db7415a679aec8a64d6f724962b8750)
    
    Change-Id: Id6961e7c35df56ca37d5cb57c2d3d1e03e31dd6d
    1282b2c3c920ef89460aa57026d89c8928155116
    18648e74d16b932cfa6fbd2057d1e9987c498fd4
    f0450bdab2c315a94a75060e1fadf2f8513f9dae
    c12ee6f4366285fc05fd59b94ec371d8ca013a4a
    7d6502c752a7b9f39f94d5cddd5f44240e0b6d04
    d15a2692f8aa572c0b5db87f04761ca3eac0249e
    e3f9e52575436b9505a412b20c7e1b24fa61f6cb
    8ae6252039729a1cba8c2fe34f72222c94e3e449
    cc3eee0776fb385a5c750293811dec796458a26b
    7e76589689d2c9e79a8823d594e8f3ca030e26cf
    cdb0694b9c1771a475368aeeb4cb11f4ba8b84f3
    e367e88df7165d728015a1b3987b220a23203765
    578343719d2d7feff51775ff1b073b6584e76f82
    0067ead587c25296ab195b80f461fb0766b1899a
    d606e58292a25309adb67cf05cc9b9a237e83f81
    e56eb27bb1827860b2600a5586e866e39cd31518
    824c29b39fe1e9e631a21f09611758bea03b0ca9
    842122f7e2cba48d4798905f41f63b3e06775d00
    ab13d67fdc218c84400a4d84e177e5096bb68379
    de44aed9a3189b0fd21a8adc039eb61bd3d3dc14
    d170132c2082fd4905192adc840125088ef5da27
    3a07361bb66a62533475618c9b50f184d791a799
    5c0800c353b46ea1fe359a893bdd8215f70cd809
    00bde6564be6fb7c7f6d2ccb1faa7dd94e414635
    9deb28a3a5420a43f8604a56145c8274bb4cc8d9
    af8e7a46be9c92a7cd11136cb903cc4b9ffe54f5
    6d02cc8dc894ef7e2903874b0ab973f7f1b3225d
    763aa5874d0c1f977decff88414eea6247f96405
    d9525918eab822b39576a1efce7dc3a3bd93e78d
    a4f2dff9c3cb81bb5056f005b23420cab1ca49e5
    7c55986fba87dea0c519f60240605db03262dfe9
    15b8aa6b95264d56018fbd5c22bcb0d04e43cf91
    7139e67e7b5bcd7e1867dff1cfbd53fa0f5748b7
    df84ababeeb109a411c03a18d3a9aa6e136c7ef4
    66fda143ba1f0fa6f2638a8bd4936c75a6c40980
    e5c0317f842258fe42f894dbace0ea0e328f288a
    aa28ef00572eb9c4ba3a6d8ae0c668b7b0132f56
    670f00b0beb77e5014c3dd4a4798e69ece882b87
    b70b5ebc498e6517ef40d0515e69e2e77288c1b5
    0c903533542608c1d7a3c97e2be7f2b7624b265b
    cac22d3c179eca42e01a724f7592ee4b34dd45e3
    54931f0dccabb2d9a58cc92ef5ebf3469f93f03c
    93669a6460806c1d4c35669a639667b0c6f3f4ab
    f615c68bfaf145d81dab96b05e7b903c1a6b03cf
    84541445e44f14244030b9970e0a48e72edacea7
    50fd30114b45d1225adb7bfd5d77cb89a229aac8
    83928638a390f434c431487b1cb13633f24de9ec
    b14639d532da5bffdfe7e0a7bb362b20a7f25dfb
    7dc7739ee7b958319ddd4e0cf262254728ef0569
    8be1b8298f096791628d1f51d36ceeabdc804f9a
    d0d9bb1d1abe6f6c9440bf979407d13db49696c4
    03748695efbe17d59aba09f88d5c6fcd174113c9
    158632de6898685e34d362cc274e959bb5c2e2d7
    1b566c835b6cdc733d0aa4393d3b3ce2ff7b764f
    b9f41091737e66995f332c06083d51408eb1497b
    
    clear objects from duplicate ids early
    
    Change-Id: I0caab9e329a465e10bf3ed321c55c287371bd437
    (cherry picked from commit 480fb6ca369f5a347489414e9d314c802b1ee7aa)
    Reviewed-on: https://gerrit.libreoffice.org/20698
    Reviewed-by: David Tardon <dtardon at redhat.com>
    Tested-by: David Tardon <dtardon at redhat.com>

diff --git a/lotuswordpro/source/filter/lwpcelllayout.cxx b/lotuswordpro/source/filter/lwpcelllayout.cxx
index 13d7307..404c4e7 100644
--- a/lotuswordpro/source/filter/lwpcelllayout.cxx
+++ b/lotuswordpro/source/filter/lwpcelllayout.cxx
@@ -328,7 +328,7 @@ XFCell* LwpCellLayout::ConvertCell(LwpObjectID aTableID, sal_uInt16 nRow, sal_uI
     // we should judt its style by current position
     if (pTable->GetDefaultCellStyle() == GetObjectID())
     {
-        aStyleName = GetCellStyleName(nRow, nCol, pTable->GetTableLayout());
+        aStyleName = GetCellStyleName(nRow, nCol, pTable->GetTableLayout().get());
     }
 
     // content of cell
@@ -423,10 +423,7 @@ LwpObjectID * LwpCellLayout::GetPreviousCellStory()
 LwpCellBorderType LwpCellLayout::GetCellBorderType(sal_uInt16 nRow, sal_uInt16 nCol, LwpTableLayout * pTableLayout)
 {
     if (!pTableLayout)
-    {
-        assert(false);
         return enumWholeBorder;
-    }
 
     // get left cell and judge if neighbour border is different
     XFBorders * pBorders = GetXFBorders();
@@ -572,8 +569,8 @@ void LwpCellLayout::RegisterDefaultCell()
  */
 void LwpCellLayout::RegisterStyle()
 {
-    LwpVirtualLayout * pParent = dynamic_cast<LwpVirtualLayout *>(GetParent().obj().get());
-    if (!pParent || pParent->GetLayoutType() != LWP_ROW_LAYOUT)
+    rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout *>(GetParent().obj().get()));
+    if (!xParent.is() || xParent->GetLayoutType() != LWP_ROW_LAYOUT)
     {
         // default cell layout, we must register 4 styles for it
         RegisterDefaultCell();
@@ -650,7 +647,7 @@ void LwpCellLayout::ApplyProtect(XFCell * pCell, LwpObjectID aTableID)
 {
     bool bProtected = false;
     // judge current cell
-    if (IsProtected())
+    if (GetIsProtected())
     {
         bProtected = true;
     }
@@ -658,7 +655,7 @@ void LwpCellLayout::ApplyProtect(XFCell * pCell, LwpObjectID aTableID)
     {
         // judge base on
         LwpCellLayout * pBase = dynamic_cast<LwpCellLayout *>(GetBasedOnStyle().get());
-        if (pBase && pBase->IsProtected())
+        if (pBase && pBase->GetIsProtected())
         {
             bProtected = true;
         }
@@ -666,9 +663,9 @@ void LwpCellLayout::ApplyProtect(XFCell * pCell, LwpObjectID aTableID)
         {
             // judge whole table
             LwpTable * pTable = dynamic_cast<LwpTable *>(aTableID.obj().get());
-            LwpTableLayout * pTableLayout = pTable ? static_cast<LwpTableLayout *>(pTable->GetTableLayout()) : NULL;
-            LwpSuperTableLayout * pSuper = pTableLayout ? pTableLayout->GetSuperTableLayout() : NULL;
-            if (pSuper && pSuper->IsProtected())
+            rtl::Reference<LwpTableLayout> xTableLayout(pTable ? pTable->GetTableLayout() : nullptr);
+            LwpSuperTableLayout * pSuper = xTableLayout.is() ? xTableLayout->GetSuperTableLayout() : nullptr;
+            if (pSuper && pSuper->GetIsProtected())
             {
                 bProtected = true;
             }
@@ -764,7 +761,11 @@ LwpCellBorderType LwpConnectedCellLayout::GetCellBorderType(sal_uInt16 nRow, sal
         }
     }
 
-    if ( (nRow + nRowSpan) == pTableLayout->GetTable()->GetRow() )
+    LwpTable* pTable = pTableLayout->GetTable();
+    if (!pTable)
+        throw std::runtime_error("missing table");
+
+    if ( (nRow + nRowSpan) == pTable->GetRow())
     {
         bNoBottomBorder = false;
     }
diff --git a/lotuswordpro/source/filter/lwpcontent.cxx b/lotuswordpro/source/filter/lwpcontent.cxx
index 77ae0d3..17be69b 100644
--- a/lotuswordpro/source/filter/lwpcontent.cxx
+++ b/lotuswordpro/source/filter/lwpcontent.cxx
@@ -125,17 +125,20 @@ void LwpContent::Read()
     pStrm->SkipExtra();
 }
 
-LwpVirtualLayout* LwpContent::GetLayout(LwpVirtualLayout* pStartLayout)
+rtl::Reference<LwpVirtualLayout> LwpContent::GetLayout(LwpVirtualLayout* pStartLayout)
 {
     return m_LayoutsWithMe.GetLayout(pStartLayout);
 }
 
 bool LwpContent::HasNonEmbeddedLayouts()
 {
-    LwpVirtualLayout* pLayout = NULL;
-    while( (pLayout = GetLayout(pLayout)) )
+    rtl::Reference<LwpVirtualLayout> xLayout;
+    while (true)
     {
-        if(!pLayout->NoContentReference())
+        xLayout = GetLayout(xLayout.get());
+        if (!xLayout.is())
+            break;
+        if (!xLayout->NoContentReference())
             return true;
     }
     return false;
@@ -143,10 +146,13 @@ bool LwpContent::HasNonEmbeddedLayouts()
 
 bool LwpContent::IsStyleContent()
 {
-    LwpVirtualLayout* pLayout = NULL;
-    while( (pLayout = GetLayout(pLayout)) )
+    rtl::Reference<LwpVirtualLayout> xLayout;
+    while (true)
     {
-        if(pLayout->IsStyleLayout())
+        xLayout = GetLayout(xLayout.get());
+        if (!xLayout.is())
+            break;
+        if (xLayout->IsStyleLayout())
             return true;
     }
     return false;
diff --git a/lotuswordpro/source/filter/lwpcontent.hxx b/lotuswordpro/source/filter/lwpcontent.hxx
index cd85bd5..b0900c2 100644
--- a/lotuswordpro/source/filter/lwpcontent.hxx
+++ b/lotuswordpro/source/filter/lwpcontent.hxx
@@ -110,7 +110,7 @@ protected:
     void Read() SAL_OVERRIDE;
 public:
     inline LwpAssociatedLayouts& GetLayoutsWithMe();
-    LwpVirtualLayout* GetLayout(LwpVirtualLayout* pStartLayout);
+    rtl::Reference<LwpVirtualLayout> GetLayout(LwpVirtualLayout* pStartLayout);
     inline bool IsActive();
     virtual bool IsTable();
     inline OUString GetClassName();
diff --git a/lotuswordpro/source/filter/lwpdivinfo.cxx b/lotuswordpro/source/filter/lwpdivinfo.cxx
index 2e97587..11d4f0e 100644
--- a/lotuswordpro/source/filter/lwpdivinfo.cxx
+++ b/lotuswordpro/source/filter/lwpdivinfo.cxx
@@ -166,10 +166,10 @@ sal_uInt16 LwpDivInfo::GetMaxNumberOfPages()
     LwpDocument* pDiv = GetDivision();
     if(!pDiv)
         return 0;
-    LwpDLVListHeadTailHolder* pHeadTail = static_cast<LwpDLVListHeadTailHolder*>(pDiv->GetPageHintsID().obj().get());
+    LwpDLVListHeadTailHolder* pHeadTail = dynamic_cast<LwpDLVListHeadTailHolder*>(pDiv->GetPageHintsID().obj().get());
     if(pHeadTail)
     {
-        LwpPageHint* pPageHint =static_cast<LwpPageHint*>(pHeadTail->GetTail().obj().get());
+        LwpPageHint* pPageHint = dynamic_cast<LwpPageHint*>(pHeadTail->GetTail().obj().get());
         if(pPageHint && !pPageHint->GetPageLayoutID().IsNull())
         {
             return pPageHint->GetPageNumber();
diff --git a/lotuswordpro/source/filter/lwpdoc.cxx b/lotuswordpro/source/filter/lwpdoc.cxx
index a0dc530..29a359b 100644
--- a/lotuswordpro/source/filter/lwpdoc.cxx
+++ b/lotuswordpro/source/filter/lwpdoc.cxx
@@ -70,7 +70,8 @@
 
 LwpDocument::LwpDocument(LwpObjectHeader& objHdr, LwpSvStream* pStrm)
     : LwpDLNFPVList(objHdr, pStrm)
-    , m_pOwnedFoundry(NULL)
+    , m_pOwnedFoundry(nullptr)
+    , m_bGettingFirstDivisionWithContentsThatIsNotOLE(false)
     , m_nFlags(0)
     , m_nPersistentFlags(0)
     , m_pLnOpts(NULL)
@@ -233,7 +234,9 @@ void LwpDocument::RegisterStyle()
 void LwpDocument::RegisterTextStyles()
 {
     //Register all text styles: para styles, character styles
-    LwpDLVListHeadHolder* pParaStyleHolder = dynamic_cast<LwpDLVListHeadHolder*>(m_pFoundry->GetTextStyleHead().obj().get());
+    LwpDLVListHeadHolder* pParaStyleHolder = m_pFoundry
+        ? dynamic_cast<LwpDLVListHeadHolder*>(m_pFoundry->GetTextStyleHead().obj().get())
+        : nullptr;
     if(pParaStyleHolder)
     {
         LwpTextStyle* pParaStyle = dynamic_cast<LwpTextStyle*> (pParaStyleHolder->GetHeadID().obj().get());
@@ -252,12 +255,15 @@ void LwpDocument::RegisterTextStyles()
  */
 void LwpDocument::RegisterLayoutStyles()
 {
-    //Register all layout styles, before register all styles in para
-    m_pFoundry->RegisterAllLayouts();
+    if (m_pFoundry)
+    {
+        //Register all layout styles, before register all styles in para
+        m_pFoundry->RegisterAllLayouts();
+    }
 
     //set initial pagelayout in story for parsing pagelayout
     LwpDivInfo* pDivInfo = dynamic_cast<LwpDivInfo*> (m_DivInfo.obj( VO_DIVISIONINFO).get());
-    if(pDivInfo)
+    if (pDivInfo)
     {
         LwpPageLayout* pPageLayout = dynamic_cast<LwpPageLayout*>(pDivInfo->GetInitialLayoutID().obj(VO_PAGELAYOUT).get());
         if(pPageLayout)
@@ -279,16 +285,18 @@ void LwpDocument::RegisterLayoutStyles()
 void LwpDocument::RegisterStylesInPara()
 {
     //Register all automatic styles in para
-    LwpHeadContent* pContent = dynamic_cast<LwpHeadContent*> (m_pFoundry->GetContentManager().GetContentList().obj().get());
-    if(pContent)
+    rtl::Reference<LwpHeadContent> xContent(m_pFoundry
+        ? dynamic_cast<LwpHeadContent*> (m_pFoundry->GetContentManager().GetContentList().obj().get())
+        : nullptr);
+    if (xContent.is())
     {
-        LwpStory* pStory = dynamic_cast<LwpStory*>(pContent->GetChildHead().obj(VO_STORY).get());
-        while(pStory)
+        rtl::Reference<LwpStory> xStory(dynamic_cast<LwpStory*>(xContent->GetChildHead().obj(VO_STORY).get()));
+        while (xStory.is())
         {
             //Register the child para
-            pStory->SetFoundry(m_pFoundry);
-            pStory->RegisterStyle();
-            pStory = dynamic_cast<LwpStory*>(pStory->GetNext().obj(VO_STORY).get());
+            xStory->SetFoundry(m_pFoundry);
+            xStory->DoRegisterStyle();
+            xStory.set(dynamic_cast<LwpStory*>(xStory->GetNext().obj(VO_STORY).get()));
         }
     }
 }
@@ -297,19 +305,20 @@ void LwpDocument::RegisterStylesInPara()
  */
 void LwpDocument::RegisterBulletStyles()
 {
+    if (!m_pFoundry)
+        return;
     //Register bullet styles
     LwpDLVListHeadHolder* mBulletHead = dynamic_cast<LwpDLVListHeadHolder*>
         (m_pFoundry->GetBulletManagerID().obj(VO_HEADHOLDER).get());
-    if( mBulletHead )
+    if (!mBulletHead)
+        return;
+    LwpSilverBullet* pBullet = dynamic_cast<LwpSilverBullet*>
+                        (mBulletHead->GetHeadID().obj().get());
+    while(pBullet)
     {
-        LwpSilverBullet* pBullet = dynamic_cast<LwpSilverBullet*>
-                            (mBulletHead->GetHeadID().obj().get());
-        while(pBullet)
-        {
-            pBullet->SetFoundry(m_pFoundry);
-            pBullet->RegisterStyle();
-            pBullet = dynamic_cast<LwpSilverBullet*> (pBullet->GetNext().obj().get());
-        }
+        pBullet->SetFoundry(m_pFoundry);
+        pBullet->RegisterStyle();
+        pBullet = dynamic_cast<LwpSilverBullet*> (pBullet->GetNext().obj().get());
     }
 }
 /**
@@ -317,13 +326,14 @@ void LwpDocument::RegisterBulletStyles()
  */
 void LwpDocument::RegisterGraphicsStyles()
 {
+    if (!m_pFoundry)
+        return;
     //Register all graphics styles, the first object should register the next;
     rtl::Reference<LwpObject> pGraphic = m_pFoundry->GetGraphicListHead().obj(VO_GRAPHIC);
-    if(pGraphic.is())
-    {
-        pGraphic->SetFoundry(m_pFoundry);
-        pGraphic->DoRegisterStyle();
-    }
+    if (!pGraphic.is())
+        return;
+    pGraphic->SetFoundry(m_pFoundry);
+    pGraphic->DoRegisterStyle();
 }
 /**
  * @descr  Register line number styles
@@ -610,7 +620,7 @@ LwpDocument* LwpDocument::GetPreviousDivision()
   /**
  * @descr    Get first division with contents that is not ole, copy from lwp-source code
  */
- LwpDocument* LwpDocument::GetFirstDivisionWithContentsThatIsNotOLE()
+ LwpDocument* LwpDocument::ImplGetFirstDivisionWithContentsThatIsNotOLE()
 {
     LwpDivInfo* pDivInfo = dynamic_cast<LwpDivInfo*>(GetDivInfoID().obj().get());
     if(pDivInfo && pDivInfo->HasContents()
@@ -619,7 +629,7 @@ LwpDocument* LwpDocument::GetPreviousDivision()
 
     LwpDocument* pDivision = GetFirstDivision();
 
-    while (pDivision && pDivision != this)
+    while (pDivision)
     {
         LwpDocument* pContentDivision = pDivision->GetFirstDivisionWithContentsThatIsNotOLE();
         if(pContentDivision)
@@ -637,7 +647,7 @@ LwpDocument* LwpDocument::GetPreviousDivision()
     LwpDocument *pLastDoc = pRoot ? pRoot->GetLastDivisionWithContents() : nullptr;
     while (pLastDoc)
     {
-        if(pLastDoc->GetEnSuperTableLayout())
+        if (pLastDoc->GetEnSuperTableLayout().is())
             return pLastDoc;
         pLastDoc = pLastDoc->GetPreviousDivisionWithContents();
     }
@@ -647,14 +657,14 @@ LwpDocument* LwpDocument::GetPreviousDivision()
  /**
  * @descr    Get endnote supertable layout, every division has only one endnote supertable layout.
  */
- LwpVirtualLayout* LwpDocument::GetEnSuperTableLayout()
+rtl::Reference<LwpVirtualLayout> LwpDocument::GetEnSuperTableLayout()
 {
     LwpHeadLayout* pHeadLayout = dynamic_cast<LwpHeadLayout*>(GetFoundry()->GetLayout().obj().get());
     if(pHeadLayout)
     {
         return pHeadLayout->FindEnSuperTableLayout();
     }
-    return NULL;
+    return rtl::Reference<LwpVirtualLayout>();
 }
 
 /**
diff --git a/lotuswordpro/source/filter/lwpdoc.hxx b/lotuswordpro/source/filter/lwpdoc.hxx
index 31b9338..91ac419 100644
--- a/lotuswordpro/source/filter/lwpdoc.hxx
+++ b/lotuswordpro/source/filter/lwpdoc.hxx
@@ -89,6 +89,7 @@ public:
 
 private:
     LwpFoundry* m_pOwnedFoundry;
+    bool m_bGettingFirstDivisionWithContentsThatIsNotOLE;
 
     //Data members in file format
     LwpObjectID m_DocSockID;
@@ -137,8 +138,7 @@ public:
     void RegisterStyle() SAL_OVERRIDE;
 
     inline bool IsChildDoc();
-    inline bool HonorProtection();
-    inline LwpObjectID& GetContentList();
+    inline bool GetHonorProtection();
     inline LwpObjectID& GetDocData();
     inline LwpObjectID& GetSocket();
 
@@ -160,12 +160,20 @@ public:
     LwpDocument* GetLastDivisionWithContents();
     LwpDocument* GetLastInGroupWithContents();
     LwpDocument* GetRootDocument();
-    LwpDocument* GetFirstDivisionWithContentsThatIsNotOLE();
+    LwpDocument* GetFirstDivisionWithContentsThatIsNotOLE()
+    {
+        if (m_bGettingFirstDivisionWithContentsThatIsNotOLE)
+            throw std::runtime_error("recursion in page divisions");
+        m_bGettingFirstDivisionWithContentsThatIsNotOLE = true;
+        LwpDocument* pRet = ImplGetFirstDivisionWithContentsThatIsNotOLE();
+        m_bGettingFirstDivisionWithContentsThatIsNotOLE = false;
+        return pRet;
+    }
     LwpDocument* GetLastDivisionThatHasEndnote();
 
     LwpDocument* GetLastDivision();
     LwpDocument* GetFirstDivision();
-    LwpVirtualLayout* GetEnSuperTableLayout();
+    rtl::Reference<LwpVirtualLayout> GetEnSuperTableLayout();
     bool GetNumberOfPages(LwpDocument* pEndDivision, sal_uInt16& nCount);
 
     sal_uInt16 GetNumberOfPagesBefore();
@@ -173,6 +181,7 @@ public:
 
 private:
     void MaxNumberOfPages(sal_uInt16& nNumPages);
+    LwpDocument* ImplGetFirstDivisionWithContentsThatIsNotOLE();
     void XFConvertFrameInPage(XFContentContainer* pCont);
     static void ChangeStyleName();
     bool IsSkippedDivision();
@@ -182,14 +191,10 @@ inline bool LwpDocument::IsChildDoc()
 {
     return (m_nPersistentFlags & DOC_CHILDDOC) != 0;
 }
-inline bool LwpDocument::HonorProtection()
+inline bool LwpDocument::GetHonorProtection()
 {
     return (m_nPersistentFlags & DOC_PROTECTED) != 0;
 }
-inline LwpObjectID& LwpDocument::GetContentList()
-{
-    return m_pFoundry->GetContentManager().GetContentList();
-}
 inline LwpObjectID& LwpDocument::GetSocket()
 {
     return m_DocSockID;
diff --git a/lotuswordpro/source/filter/lwpfnlayout.cxx b/lotuswordpro/source/filter/lwpfnlayout.cxx
index e02c597..d50cc0e 100644
--- a/lotuswordpro/source/filter/lwpfnlayout.cxx
+++ b/lotuswordpro/source/filter/lwpfnlayout.cxx
@@ -119,14 +119,14 @@ void LwpFnRowLayout::RegisterStyle()
 {
     // register cells' style
     LwpObjectID& rCellID = GetChildHead();
-    LwpCellLayout * pCellLayout = static_cast<LwpCellLayout *>(rCellID.obj().get());
+    LwpCellLayout * pCellLayout = dynamic_cast<LwpCellLayout *>(rCellID.obj().get());
 
     while(pCellLayout)
     {
         pCellLayout->SetFoundry(m_pFoundry);
         pCellLayout->RegisterStyle();
         rCellID = pCellLayout->GetNext();
-        pCellLayout = static_cast<LwpCellLayout *>(rCellID.obj().get());
+        pCellLayout = dynamic_cast<LwpCellLayout *>(rCellID.obj().get());
     }
 }
 
@@ -199,14 +199,14 @@ void LwpEndnoteLayout::RegisterStyle()
 {
     // register style of rows
     LwpObjectID& rRowID = GetChildHead();
-    LwpRowLayout * pRowLayout = static_cast<LwpRowLayout *>(rRowID.obj().get());
+    LwpRowLayout * pRowLayout = dynamic_cast<LwpRowLayout *>(rRowID.obj().get());
     while (pRowLayout)
     {
         pRowLayout->SetFoundry(m_pFoundry);
         pRowLayout->RegisterStyle();
 
         rRowID = pRowLayout->GetNext();
-        pRowLayout = static_cast<LwpRowLayout *>(rRowID.obj().get());
+        pRowLayout = dynamic_cast<LwpRowLayout *>(rRowID.obj().get());
     }
 }
 
@@ -262,7 +262,7 @@ void LwpEnSuperTableLayout::XFConvert(XFContentContainer * /*pCont*/)
 
     while(!rID.IsNull())
     {
-        LwpVirtualLayout * pLayout = static_cast<LwpVirtualLayout *>(rID.obj().get());
+        LwpVirtualLayout * pLayout = dynamic_cast<LwpVirtualLayout*>(rID.obj().get());
         if(!pLayout)
         {
             break;
@@ -318,7 +318,7 @@ LwpVirtualLayout* LwpFnSuperTableLayout::GetMainTableLayout()
 
     while(!rID.IsNull())
     {
-        LwpVirtualLayout * pLayout = static_cast<LwpVirtualLayout *>(rID.obj().get());
+        LwpVirtualLayout * pLayout = dynamic_cast<LwpVirtualLayout *>(rID.obj().get());
         if(!pLayout)
         {
             break;
diff --git a/lotuswordpro/source/filter/lwpfootnote.cxx b/lotuswordpro/source/filter/lwpfootnote.cxx
index 86c69ff..0964eda 100644
--- a/lotuswordpro/source/filter/lwpfootnote.cxx
+++ b/lotuswordpro/source/filter/lwpfootnote.cxx
@@ -206,7 +206,7 @@ LwpCellLayout* LwpFootnote::GetCellLayout()
     LwpEnSuperTableLayout* pEnSuperLayout = FindFootnoteTableLayout();
     if(pEnSuperLayout)
     {
-        LwpTableLayout* pTableLayout = static_cast<LwpTableLayout*>(pEnSuperLayout->GetMainTableLayout());
+        LwpTableLayout* pTableLayout = dynamic_cast<LwpTableLayout*>(pEnSuperLayout->GetMainTableLayout());
         if(pTableLayout)
         {
             LwpRowLayout* pRowLayout = pTableLayout->GetRowLayout(m_nRow);
@@ -372,7 +372,7 @@ LwpEnSuperTableLayout* LwpFootnote::FindFootnoteTableLayout()
 
     while ((pContent = pFoundry->EnumContents(pContent)) != NULL)
         if (pContent->IsTable() && (strClassName.equals(pContent->GetClassName())) &&
-            pContent->IsActive() && pContent->GetLayout(NULL))
+            pContent->IsActive() && pContent->GetLayout(nullptr).is())
         {
             // Found it!
             return static_cast<LwpEnSuperTableLayout *>(
@@ -390,7 +390,7 @@ LwpContent* LwpFootnote::FindFootnoteContent()
     LwpContent* pContent = dynamic_cast<LwpContent*>(m_Content.obj().get());
     //if the content has layout, the content has footnote contents;
     //or looking for the celllayout and return the footnote contents.
-    if(pContent && pContent->GetLayout(NULL))
+    if (pContent && pContent->GetLayout(nullptr).is())
         return pContent;
 
     LwpCellLayout* pCellLayout = GetCellLayout();
diff --git a/lotuswordpro/source/filter/lwpfoundry.cxx b/lotuswordpro/source/filter/lwpfoundry.cxx
index 3e2609f..1d44e18 100644
--- a/lotuswordpro/source/filter/lwpfoundry.cxx
+++ b/lotuswordpro/source/filter/lwpfoundry.cxx
@@ -233,18 +233,21 @@ void LwpFoundry::RegisterAllLayouts()
 
 LwpBookMark* LwpFoundry::GetBookMark(LwpObjectID objMarker)
 {
-    LwpDLVListHeadHolder* pHeadHolder= static_cast
+    LwpDLVListHeadHolder* pHeadHolder= dynamic_cast
                     <LwpDLVListHeadHolder*>(m_BookMarkHead.obj().get());
+
+    if (!pHeadHolder)
+        return nullptr;
+
     LwpObjectID& rObjID = pHeadHolder->GetHeadID();
-    LwpBookMark* pBookMark;
-    pBookMark = static_cast<LwpBookMark*>(rObjID.obj().get());
+    LwpBookMark* pBookMark = dynamic_cast<LwpBookMark*>(rObjID.obj().get());
 
     while (pBookMark)
     {
         if (pBookMark->IsRightMarker(objMarker))
             return pBookMark;
         rObjID = pBookMark->GetNext();
-        pBookMark = static_cast<LwpBookMark*>(rObjID.obj().get());
+        pBookMark = dynamic_cast<LwpBookMark*>(rObjID.obj().get());
     }
     return NULL;
 }
@@ -273,7 +276,7 @@ LwpSection* LwpFoundry::EnumSections(LwpSection * pSection)
 */
 LwpObjectID * LwpFoundry::GetDefaultTextStyle()
 {
-    LwpVersionedPointer * pPointer = static_cast<LwpVersionedPointer *>(m_DefaultTextStyle.obj().get());
+    LwpVersionedPointer * pPointer = dynamic_cast<LwpVersionedPointer *>(m_DefaultTextStyle.obj().get());
     if (!pPointer)
         return NULL;
 
@@ -287,16 +290,16 @@ LwpObjectID * LwpFoundry::GetDefaultTextStyle()
 LwpObjectID * LwpFoundry::FindParaStyleByName(const OUString& name)
 {
     //Register all text styles: para styles, character styles
-    LwpDLVListHeadHolder* pParaStyleHolder = static_cast<LwpDLVListHeadHolder*>(GetTextStyleHead().obj().get());
+    LwpDLVListHeadHolder* pParaStyleHolder = dynamic_cast<LwpDLVListHeadHolder*>(GetTextStyleHead().obj().get());
     if(pParaStyleHolder)
     {
-        LwpTextStyle* pParaStyle = static_cast<LwpTextStyle*> (pParaStyleHolder->GetHeadID().obj().get());
+        LwpTextStyle* pParaStyle = dynamic_cast<LwpTextStyle*> (pParaStyleHolder->GetHeadID().obj().get());
         while(pParaStyle)
         {
             OUString strName = pParaStyle->GetName().str();
             if(strName == name)
                 return &pParaStyle->GetObjectID();
-            pParaStyle = static_cast<LwpTextStyle*>(pParaStyle->GetNext().obj().get());
+            pParaStyle = dynamic_cast<LwpTextStyle*>(pParaStyle->GetNext().obj().get());
         }
     }
 
@@ -405,8 +408,8 @@ LwpContent* LwpContentManager::EnumContents(LwpContent* pContent)
 {
     if(pContent)
         return pContent->GetNextEnumerated();
-    LwpVersionedPointer* pPointer = static_cast<LwpVersionedPointer*>(m_EnumHead.obj().get());
-    return pPointer ? static_cast<LwpContent*>(pPointer->GetPointer().obj().get()) : NULL;
+    LwpVersionedPointer* pPointer = dynamic_cast<LwpVersionedPointer*>(m_EnumHead.obj().get());
+    return pPointer ? dynamic_cast<LwpContent*>(pPointer->GetPointer().obj().get()) : nullptr;
 }
 
 void LwpPieceManager::Read(LwpObjectStream *pStrm)
@@ -457,14 +460,14 @@ LwpOrderedObject* LwpOrderedObjectManager::Enumerate(LwpOrderedObject * pLast)
 {
     // If Last has a next, return it.
     if(pLast && !pLast->GetNext().IsNull())
-        return static_cast<LwpOrderedObject*>(pLast->GetNext().obj().get());
+        return dynamic_cast<LwpOrderedObject*>(pLast->GetNext().obj().get());
 
     LwpListList* pList = NULL;
     if(pLast)
     {
         // We're at the end of Last's list (not Liszt's list).
         // Start with the next active list
-        pList = static_cast<LwpListList*>(pLast->GetListList().obj().get());
+        pList = dynamic_cast<LwpListList*>(pLast->GetListList().obj().get());
         pList= GetNextActiveListList(pList);
     }
     else
@@ -475,7 +478,7 @@ LwpOrderedObject* LwpOrderedObjectManager::Enumerate(LwpOrderedObject * pLast)
 
     if(pList)
     {
-        return static_cast<LwpOrderedObject*>(pList->GetHead().obj().get());
+        return dynamic_cast<LwpOrderedObject*>(pList->GetHead().obj().get());
     }
 
     return NULL;
@@ -489,23 +492,22 @@ LwpListList* LwpOrderedObjectManager::GetNextActiveListList(LwpListList * pLast)
 {
     LwpListList* pList = NULL;
     if(pLast)
-        pList = static_cast<LwpListList*>(pLast->GetNext().obj().get());
+        pList = dynamic_cast<LwpListList*>(pLast->GetNext().obj().get());
     else
     {
-        LwpDLVListHeadHolder* pHeadHolder= static_cast<LwpDLVListHeadHolder*>(m_Head.obj().get());
+        LwpDLVListHeadHolder* pHeadHolder= dynamic_cast<LwpDLVListHeadHolder*>(m_Head.obj().get());
         if(pHeadHolder)
         {
-            pList = static_cast<LwpListList*>(pHeadHolder->GetHeadID().obj().get());
+            pList = dynamic_cast<LwpListList*>(pHeadHolder->GetHeadID().obj().get());
         }
     }
 
     while(pList)
     {
-        LwpContent* pContent = static_cast<LwpContent*>(pList->GetObject().obj().get());
-        if(pContent && pContent->HasNonEmbeddedLayouts() &&
-            !pContent->IsStyleContent())
+        LwpContent* pContent = dynamic_cast<LwpContent*>(pList->GetObject().obj().get());
+        if (pContent && pContent->HasNonEmbeddedLayouts() && !pContent->IsStyleContent())
             return pList;
-        pList = static_cast<LwpListList*>(pList->GetNext().obj().get());
+        pList = dynamic_cast<LwpListList*>(pList->GetNext().obj().get());
     }
     return NULL;
 }
diff --git a/lotuswordpro/source/filter/lwpframelayout.cxx b/lotuswordpro/source/filter/lwpframelayout.cxx
index feff262..19944f7 100644
--- a/lotuswordpro/source/filter/lwpframelayout.cxx
+++ b/lotuswordpro/source/filter/lwpframelayout.cxx
@@ -190,35 +190,35 @@ void  LwpFrame::RegisterStyle(XFFrameStyle* pFrameStyle)
  void LwpFrame::XFConvert(XFContentContainer* pCont)
  {
     // parse the frame which anchor to page
-    LwpVirtualLayout* pParent = m_pLayout->GetParentLayout();
-    if (!pParent)
+    rtl::Reference<LwpVirtualLayout> xParent = m_pLayout->GetParentLayout();
+    if (!xParent.is())
         throw std::runtime_error("missing Parent Layout");
-    if(pParent->IsPage()&& pParent->GetParentLayout()->IsPage())
+    if (xParent->IsPage() && xParent->GetParentLayout().is() && xParent->GetParentLayout()->IsPage())
     {
         //for mirror page, problems exist if the parent layout is header or footer layout,
-        pParent = pParent->GetParentLayout();
+        xParent = xParent->GetParentLayout();
     }
-    if(m_pLayout->IsAnchorPage()&& pParent->IsPage())
+    if(m_pLayout->IsAnchorPage()&& xParent->IsPage())
     {
         //get parent layout
         if(m_pLayout->IsUseOnPage())
         {
-            sal_Int32 nPageNo = pParent->GetPageNumber(m_pLayout->GetUsePage());
+            sal_Int32 nPageNo = xParent->GetPageNumber(m_pLayout->GetUsePage());
             if(nPageNo>0)
                 m_pLayout->XFConvertFrame(pCont, nPageNo);
         }
         else if(m_pLayout->IsUseOnAllPages())
         {
-            sal_Int32 nFirst = pParent->GetPageNumber(FIRST_LAYOUTPAGENO);
-            sal_Int32 nLast = pParent->GetPageNumber(LAST_LAYOUTPAGENO);
+            sal_Int32 nFirst = xParent->GetPageNumber(FIRST_LAYOUTPAGENO);
+            sal_Int32 nLast = xParent->GetPageNumber(LAST_LAYOUTPAGENO);
             if(nLast > 0)
                 m_pLayout->XFConvertFrame(pCont, nFirst, nLast, true);
 
         }
         else if(m_pLayout->IsUseOnAllOddPages()||m_pLayout->IsUseOnAllEvenPages())
         {
-            sal_Int32 nFirst = pParent->GetPageNumber(FIRST_LAYOUTPAGENO);
-            sal_Int32 nLast = pParent->GetPageNumber(LAST_LAYOUTPAGENO);
+            sal_Int32 nFirst = xParent->GetPageNumber(FIRST_LAYOUTPAGENO);
+            sal_Int32 nLast = xParent->GetPageNumber(LAST_LAYOUTPAGENO);
             if(nLast > 0)
             {
                 sal_uInt16 first = static_cast<sal_uInt16>(nFirst);
@@ -257,7 +257,8 @@ void LwpFrame::ApplyWrapType(XFFrameStyle *pFrameStyle)
             //between the frame object and page margins
 
             eWrap = enumXFWrapBest;
-            LwpMiddleLayout* pParent = static_cast<LwpMiddleLayout*>(m_pLayout->GetContainerLayout());
+            rtl::Reference<LwpVirtualLayout> xContainer(m_pLayout->GetContainerLayout());
+            LwpMiddleLayout* pParent = dynamic_cast<LwpMiddleLayout*>(xContainer.get());
             if(pParent)
             {
                 if(IsLeftWider())
@@ -275,7 +276,7 @@ void LwpFrame::ApplyWrapType(XFFrameStyle *pFrameStyle)
         case LwpPlacableLayout::LAY_NO_WRAP_AROUND:
         {
             eWrap = enumXFWrapRunThrough;
-            if(!m_pLayout->GetBackColor() && !m_pLayout->GetWaterMarkLayout())
+            if(!m_pLayout->GetBackColor() && !m_pLayout->GetWaterMarkLayout().is())
             {
                 //pFrameStyle->SetBackGround(sal_True);
                 XFColor aXFColor(0xffffff); //white color
@@ -400,7 +401,7 @@ void LwpFrame::ApplyBackColor(XFFrameStyle* pFrameStyle)
 */
 void LwpFrame::ApplyProtect(XFFrameStyle* pFrameStyle)
 {
-    if(m_pLayout->IsProtected())
+    if(m_pLayout->GetIsProtected())
     {
         pFrameStyle->SetProtect(true,true,true);
     }
@@ -437,8 +438,8 @@ void LwpFrame::ApplyPosType(XFFrameStyle* pFrameStyle)
             //set vertical position
             if(m_pLayout->IsAnchorPage())//in page
             {
-                LwpVirtualLayout* pContainer = m_pLayout->GetContainerLayout();
-                if(pContainer && (pContainer->IsHeader() || pContainer->IsFooter()))
+                rtl::Reference<LwpVirtualLayout> xContainer(m_pLayout->GetContainerLayout());
+                if (xContainer.is() && (xContainer->IsHeader() || xContainer->IsFooter()))
                 {
                     //Only anchor to para, the frame can display in header and footer of each page
                     eYPos = enumXFFrameYPosFromTop; //from top
@@ -470,15 +471,15 @@ void LwpFrame::ApplyPosType(XFFrameStyle* pFrameStyle)
             eYPos = enumXFFrameYPosBelow;   //below
             eYRel = enumXFFrameYRelChar; //from char
             //set vertical position
-            LwpVirtualLayout* pContainer = m_pLayout->GetContainerLayout();
-            if(pContainer && pContainer->IsPage())//in page
+            rtl::Reference<LwpVirtualLayout> xContainer(m_pLayout->GetContainerLayout());
+            if (xContainer.is() && xContainer->IsPage())//in page
             {
                 //eYPos = enumXFFrameYPosFromTop;
                 //eYRel = enumXFFrameYRelPage;
                 eYPos = enumXFFrameYPosBelow;
                 eYRel = enumXFFrameYRelChar;
             }
-            else if(pContainer && pContainer->IsFrame()) //in frame
+            else if (xContainer.is() && xContainer->IsFrame()) //in frame
             {
                 eYPos = enumXFFrameYPosFromTop;
                 eYRel = enumXFFrameYRelPage;
@@ -539,12 +540,13 @@ void LwpFrame::ApplyWatermark(XFFrameStyle *pFrameStyle)
     {
         pFrameStyle->SetBackImage(pBGImage);
         //set watermark transparent
-         LwpMiddleLayout* pLay = static_cast<LwpMiddleLayout*>(m_pLayout->GetWaterMarkLayout());
-         LwpBackgroundStuff* pBackgroundStuff = pLay->GetBackgroundStuff();
-         if(pBackgroundStuff && !pBackgroundStuff->IsTransparent())
-         {
-             pFrameStyle->SetTransparency(100);
-         }
+        rtl::Reference<LwpVirtualLayout> xWaterMarkLayout(m_pLayout->GetWaterMarkLayout());
+        LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xWaterMarkLayout.get());
+        LwpBackgroundStuff* pBackgroundStuff = pLay ? pLay->GetBackgroundStuff() : nullptr;
+        if(pBackgroundStuff && !pBackgroundStuff->IsTransparent())
+        {
+            pFrameStyle->SetTransparency(100);
+        }
      }
 }
 
@@ -617,11 +619,11 @@ void LwpFrame::ParseAnchorType(XFFrame *pXFFrame)
             //anchor to page, frame and cell
             if(m_pLayout->IsAnchorPage())//in page
             {
-                LwpVirtualLayout* pContainer = m_pLayout->GetContainerLayout();
-                if(pContainer && (pContainer->IsHeader() || pContainer->IsFooter()))
+                rtl::Reference<LwpVirtualLayout> xContainer(m_pLayout->GetContainerLayout());
+                if (xContainer.is() && (xContainer->IsHeader() || xContainer->IsFooter()))
                 {
                     eAnchor = enumXFAnchorPara;
-                    fYOffset -= pContainer->GetMarginsValue(MARGIN_TOP);
+                    fYOffset -= xContainer->GetMarginsValue(MARGIN_TOP);
                 }
                 else
                     eAnchor = enumXFAnchorPage;
@@ -634,8 +636,9 @@ void LwpFrame::ParseAnchorType(XFFrame *pXFFrame)
             {
                 //eAnchor = enumXFAnchorChar;
                 eAnchor = enumXFAnchorPara;
-                LwpMiddleLayout* pContainer = static_cast<LwpMiddleLayout*>(m_pLayout->GetContainerLayout());
-                if(pContainer)
+                rtl::Reference<LwpVirtualLayout> xContainer(m_pLayout->GetContainerLayout());
+                LwpMiddleLayout* pContainer = dynamic_cast<LwpMiddleLayout*>(xContainer.get());
+                if (pContainer)
                 {
                     fYOffset -= pContainer->GetMarginsValue(MARGIN_TOP);
                 }
@@ -645,26 +648,26 @@ void LwpFrame::ParseAnchorType(XFFrame *pXFFrame)
         case LwpLayoutRelativityGuts::LAY_PARA_RELATIVE:    //same page as text
         {
             eAnchor = enumXFAnchorChar;
-            LwpVirtualLayout* pContainer = m_pLayout->GetContainerLayout();
-            if(pContainer && pContainer->IsPage())//in page
+            rtl::Reference<LwpVirtualLayout> xContainer(m_pLayout->GetContainerLayout());
+            if (xContainer.is() && xContainer->IsPage())//in page
             {
                 //eAnchor = enumXFAnchorPage;
                 eAnchor = enumXFAnchorChar;// to character
             }
-            else if(pContainer && pContainer->IsFrame()) //in frame
+            else if (xContainer.is() && xContainer->IsFrame()) //in frame
             {
                 eAnchor = enumXFAnchorFrame;
             }
-            else if(pContainer && pContainer->IsCell()) //in cell
+            else if (xContainer.is() && xContainer->IsCell()) //in cell
             {
                 //eAnchor = enumXFAnchorChar;
                 eAnchor = enumXFAnchorPara;
-                fYOffset -= pContainer->GetMarginsValue(MARGIN_TOP);
+                fYOffset -= xContainer->GetMarginsValue(MARGIN_TOP);
             }
-            else if(pContainer && (pContainer->IsHeader() || pContainer->IsFooter()))//in header or footer
+            else if (xContainer.is() && (xContainer->IsHeader() || xContainer->IsFooter()))//in header or footer
             {
                 eAnchor = enumXFAnchorPara;
-                fYOffset -= pContainer->GetMarginsValue(MARGIN_TOP);
+                fYOffset -= xContainer->GetMarginsValue(MARGIN_TOP);
             }
             break;
         }
@@ -692,7 +695,6 @@ void LwpFrame::ParseAnchorType(XFFrame *pXFFrame)
 
             //because of the different feature between Word Pro and SODC, I simulate the vertical base offset
             //between anchor and frame origin using the font height.
-            //LwpPara* pPara = static_cast<LwpPara*>(m_pLayout->GetPosition()->obj());
             rtl::Reference<XFFont> pFont = m_pLayout->GetFont();
             if(pFont.is())
             {
@@ -717,9 +719,9 @@ void LwpFrame::ParseAnchorType(XFFrame *pXFFrame)
  */
 bool LwpFrame::IsLeftWider()
 {
-    //LwpMiddleLayout* pParent = static_cast<LwpMiddleLayout*>(m_pLayout->GetContainerLayout());
-    LwpVirtualLayout* pParent = static_cast<LwpVirtualLayout*>(m_pLayout->GetContainerLayout());
-    if(pParent)
+    rtl::Reference<LwpVirtualLayout> xLayout(m_pLayout->GetContainerLayout());
+    LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*>(xLayout.get());
+    if (pParent)
     {
         LwpPoint aPoint = m_pLayout->GetOrigin();
         double fXOffset = LwpTools::ConvertFromUnitsToMetric(aPoint.GetX());
@@ -917,10 +919,13 @@ bool LwpFrameLayout::IsForWaterMark()
 {
     if(m_nBuoyancy >=LAY_BUOYLAYER)
     {
-        if(!m_Content.IsNull() && (m_Content.obj()->GetTag()==VO_GRAPHIC) )
-        {
+        if (m_Content.IsNull())
+            return false;
+        rtl::Reference<LwpObject> content = m_Content.obj();
+        if (!content.is())
+            return false;
+        if (content->GetTag() == VO_GRAPHIC)
             return true;
-        }
     }
     return false;
 }
@@ -947,8 +952,9 @@ double LwpFrameLayout::GetWidth()
 double LwpFrameLayout::GetMaxWidth()
 {
     double fActualWidth = 0;
-    LwpMiddleLayout* pParent = static_cast<LwpMiddleLayout*>(GetContainerLayout());
-    if(pParent)
+    rtl::Reference<LwpVirtualLayout> xLayout(GetContainerLayout());
+    LwpMiddleLayout* pParent = dynamic_cast<LwpMiddleLayout*>(xLayout.get());
+    if (pParent)
     {
         LwpPoint aPoint = GetOrigin();
         double fXOffset = LwpTools::ConvertFromUnitsToMetric(aPoint.GetX());
@@ -1098,11 +1104,11 @@ void LwpGroupLayout::XFConvertFrame(XFContentContainer* pCont, sal_Int32 nStart
         m_pFrame->Parse(pXFFrame, nStart);
 
         //add child frame into group
-        LwpVirtualLayout* pLayout = static_cast<LwpVirtualLayout*>(GetChildHead().obj().get());
+        LwpVirtualLayout* pLayout = dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get());
         while(pLayout)
         {
             pLayout->XFConvert(pXFFrame);
-            pLayout = static_cast<LwpVirtualLayout*>(pLayout->GetNext().obj().get());
+            pLayout = dynamic_cast<LwpVirtualLayout*>(pLayout->GetNext().obj().get());
         }
 
         pCont ->Add(pXFFrame);
@@ -1181,12 +1187,12 @@ void LwpDropcapLayout::RegisterStyle(LwpFoundry* pFoundry)
     {
         pStory->SetDropcapFlag(true);
         pStory->SetFoundry(pFoundry);
-        LwpPara* pPara = static_cast<LwpPara*>(pStory->GetFirstPara().obj().get());
+        LwpPara* pPara = dynamic_cast<LwpPara*>(pStory->GetFirstPara().obj().get());
         while(pPara)
         {
             pPara->SetFoundry(pFoundry);
             pPara->RegisterStyle();
-            pPara = static_cast<LwpPara*>(pPara->GetNext().obj().get());
+            pPara = dynamic_cast<LwpPara*>(pPara->GetNext().obj().get());
         }
     }
 }
diff --git a/lotuswordpro/source/filter/lwpfrib.cxx b/lotuswordpro/source/filter/lwpfrib.cxx
index d88ec39..e1a9b8c 100644
--- a/lotuswordpro/source/filter/lwpfrib.cxx
+++ b/lotuswordpro/source/filter/lwpfrib.cxx
@@ -261,7 +261,7 @@ void LwpFrib::RegisterStyle(LwpFoundry* pFoundry)
         {
             pStyle = new XFTextStyle();
             *pStyle = *pNamedStyle;
-            LwpCharacterStyle* pCharStyle = static_cast<LwpCharacterStyle*>(m_pModifiers->CharStyleID.obj().get());
+            LwpCharacterStyle* pCharStyle = dynamic_cast<LwpCharacterStyle*>(m_pModifiers->CharStyleID.obj().get());
 
             pStyle->SetStyleName("");
             pFont = pFoundry->GetFontManger().CreateOverrideFont(pCharStyle->GetFinalFontID(),m_pModifiers->FontID);
diff --git a/lotuswordpro/source/filter/lwpfribbreaks.cxx b/lotuswordpro/source/filter/lwpfribbreaks.cxx
index eaaf919..c6ebad1 100644
--- a/lotuswordpro/source/filter/lwpfribbreaks.cxx
+++ b/lotuswordpro/source/filter/lwpfribbreaks.cxx
@@ -80,12 +80,9 @@ void LwpFribColumnBreak::RegisterBreakStyle(LwpPara * pPara)
     *pOverStyle = *pBaseStyle;
     pOverStyle->SetStyleName("");
 
-    //Old code
-    //if (static_cast<LwpStory*>(pPara->GetStoryID()->obj())
-    //  ->GetCurrentLayout()->GetNumCols() == 1)
     //New code
-    LwpStory* pStory = static_cast<LwpStory*>(pPara->GetStoryID().obj().get());
-    LwpPageLayout* pCurLayout = pStory ? pStory->GetCurrentLayout() : NULL;
+    LwpStory* pStory = dynamic_cast<LwpStory*>(pPara->GetStoryID().obj().get());
+    LwpPageLayout* pCurLayout = pStory ? pStory->GetCurrentLayout() : nullptr;
     if( pCurLayout && (pCurLayout->GetNumCols() == 1) )
 
     {
@@ -129,7 +126,7 @@ void LwpFribPageBreak::RegisterBreakStyle(LwpPara* pPara)
     XFParaStyle* pBaseStyle =  pPara->GetXFParaStyle();
     if (pBaseStyle == NULL) return;
 
-    LwpPageLayout* pLayout = static_cast<LwpPageLayout*>(m_Layout.obj().get());
+    LwpPageLayout* pLayout = dynamic_cast<LwpPageLayout*>(m_Layout.obj().get());
     if(pLayout)
     {
         m_pMasterPage = new LwpMasterPage(pPara, pLayout);
diff --git a/lotuswordpro/source/filter/lwpfribframe.cxx b/lotuswordpro/source/filter/lwpfribframe.cxx
index 9f0161e..6b1d5f8 100644
--- a/lotuswordpro/source/filter/lwpfribframe.cxx
+++ b/lotuswordpro/source/filter/lwpfribframe.cxx
@@ -156,13 +156,13 @@ void LwpFribFrame::XFConvert(XFContentContainer* pCont)
     sal_uInt8 nType = pLayout->GetRelativeType();
     if( LwpLayoutRelativityGuts::LAY_PARA_RELATIVE == nType)
     {
-        LwpVirtualLayout* pContainerLayout = pLayout->GetContainerLayout();
-        if(pContainerLayout && pContainerLayout->IsFrame())
+        rtl::Reference<LwpVirtualLayout> xContainerLayout(pLayout->GetContainerLayout());
+        if (xContainerLayout.is() && xContainerLayout->IsFrame())
         {
             //same page as text and in frame
             pXFContentContainer = m_pPara->GetXFContainer();
         }
-        else if(pContainerLayout && pContainerLayout->IsCell())
+        else if (xContainerLayout.is() && xContainerLayout->IsCell())
         {
             //same page as text and in cell, get the first xfpara
             rtl::Reference<XFContent> first(
diff --git a/lotuswordpro/source/filter/lwpfribsection.cxx b/lotuswordpro/source/filter/lwpfribsection.cxx
index e24faf8..e14993c 100644
--- a/lotuswordpro/source/filter/lwpfribsection.cxx
+++ b/lotuswordpro/source/filter/lwpfribsection.cxx
@@ -102,7 +102,7 @@ void LwpFribSection::Read(LwpObjectStream *pObjStrm, sal_uInt16 /*len*/)
  */
 LwpSection* LwpFribSection::GetSection()
 {
-    return static_cast<LwpSection*>(m_Section.obj().get());
+    return dynamic_cast<LwpSection*>(m_Section.obj().get());
 }
 
 /**
@@ -126,11 +126,12 @@ void LwpFribSection::RegisterSectionStyle()
 void LwpFribSection::SetSectionName()
 {
     LwpSection* pSection = GetSection();
-    if(pSection)
-    {
-        LwpStory* pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
-        pStory->SetSectionName(pSection->GetSectionName());
-    }
+    if (!pSection)
+        return;
+    LwpStory* pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
+    if (!pStory)
+        return;
+    pStory->SetSectionName(pSection->GetSectionName());
 }
 
 /**
@@ -164,7 +165,7 @@ void LwpFribSection::ParseSection()
             m_pMasterPage->ParseSection(this);
         }
     }
-    else if (LwpStory* pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get()))
+    else if (LwpStory* pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get()))
     {
         rtl::Reference<LwpObject> xObj(m_Section.obj());
         if (xObj.is() && xObj->GetTag() == VO_INDEXSECTION)
@@ -172,16 +173,6 @@ void LwpFribSection::ParseSection()
             //create a new section and add it to container
             XFIndex* pIndex = new XFIndex;
             pIndex->SetIndexType(enumXFIndexAlphabetical);
-            /*
-            sal_Bool bRunin = sal_False;
-            sal_Bool bSeparator = sal_False;
-            LwpIndexSection* pIndexSection = static_cast<LwpIndexSection*>(m_Section.obj());
-            if (pIndexSection->IsFormatRunin())
-                bRunin = sal_True;
-            if (pIndexSection->IsFormatSeparator())
-                bSeparator = sal_True;
-            pIndex->SetDefaultAlphaIndex("",bRunin,bSeparator);
-            */
             SetDefaultAlphaIndex(pIndex);
 
             pStory->AddXFContent( pIndex );
@@ -200,7 +191,7 @@ void LwpFribSection::SetDefaultAlphaIndex(XFIndex * pXFIndex)
     LwpFoundry* pFoundry = m_pPara->GetFoundry();
     OUString styleName = pFoundry->FindActuralStyleName("Separator");
 
-    LwpIndexSection* pIndexSection = static_cast<LwpIndexSection*>(m_Section.obj().get());
+    LwpIndexSection* pIndexSection = dynamic_cast<LwpIndexSection*>(m_Section.obj().get());
     XFIndexTemplate * pTemplateSep = new XFIndexTemplate();
     if (pIndexSection->IsFormatSeparator())
     {
@@ -259,7 +250,7 @@ bool LwpMasterPage::RegisterMasterPage(LwpFrib* pFrib)
     //if there is no other frib after current firb, register master page in starting para of next page
     if(IsNextPageType()&&(!pFrib->HasNextFrib()))
     {
-        LwpStory* pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
+        LwpStory* pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
         pStory->SetCurrentLayout(m_pLayout);
         RegisterFillerPageStyle();
         return false;
@@ -290,7 +281,7 @@ bool LwpMasterPage::RegisterMasterPage(LwpFrib* pFrib)
         case LwpLayout::StartOnOddPage: //fall through
         case LwpLayout::StartOnEvenPage:
         {
-            LwpStory* pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
+            LwpStory* pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
             pStory->SetCurrentLayout(m_pLayout);
             //get odd page layout when the current pagelayout is mirror
             m_pLayout = pStory->GetCurrentLayout();
@@ -305,7 +296,7 @@ bool LwpMasterPage::RegisterMasterPage(LwpFrib* pFrib)
     }
 
     //register tab style;
-    LwpStory* pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
+    LwpStory* pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
     pStory->SetTabLayout(m_pLayout);
     m_pPara->RegisterTabStyle(pOverStyle);
 
@@ -316,7 +307,7 @@ bool LwpMasterPage::RegisterMasterPage(LwpFrib* pFrib)
     {
         XFSectionStyle* pSectStyle= new XFSectionStyle();
         //set margin
-        pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
+        pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
         if(pStory)
         {
             LwpPageLayout* pCurrentLayout = pStory->GetCurrentLayout();
@@ -348,7 +339,7 @@ bool LwpMasterPage::IsNeedSection()
 {
     bool bNewSection = false;
     //get story
-    LwpStory* pStory = static_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
+    LwpStory* pStory = dynamic_cast<LwpStory*>(m_pPara->GetStoryID().obj().get());
     //if pagelayout is modified, register the pagelayout
     if(pStory->IsPMModified())
     {
@@ -396,8 +387,8 @@ void LwpMasterPage::ParseSection(LwpFrib* pFrib)
     XFContentContainer* pContent = CreateXFSection();
     if(pContent)
     {
-        LwpStory* pStory = static_cast<LwpStory*> ( m_pPara->GetStoryID().obj().get() );
-        //delete the additional blank para, 06/28/2005
+        LwpStory* pStory = dynamic_cast<LwpStory*> ( m_pPara->GetStoryID().obj().get() );
+        //delete the additional blank para
         XFParagraph* pCurrPara = rFribPtr.GetXFPara();
         if(!pCurrPara->HasContents())
         {
@@ -412,7 +403,7 @@ void LwpMasterPage::ParseSection(LwpFrib* pFrib)
     }
     else
     {
-        LwpStory* pStory = static_cast<LwpStory*> ( m_pPara->GetStoryID().obj().get() );
+        LwpStory* pStory = dynamic_cast<LwpStory*> ( m_pPara->GetStoryID().obj().get() );
         pContent = pStory->GetXFContent();
     }
     if(pContent)
diff --git a/lotuswordpro/source/filter/lwpfribtable.cxx b/lotuswordpro/source/filter/lwpfribtable.cxx
index 86c70ea..6066592 100644
--- a/lotuswordpro/source/filter/lwpfribtable.cxx
+++ b/lotuswordpro/source/filter/lwpfribtable.cxx
@@ -73,7 +73,7 @@ void LwpFribTable::Read(LwpObjectStream* pObjStrm, sal_uInt16 /*len*/)
 
 LwpSuperTableLayout* LwpFribTable::GetSuperTable()
 {
-    return static_cast<LwpSuperTableLayout*>(m_objTable.obj().get());
+    return dynamic_cast<LwpSuperTableLayout*>(m_objTable.obj().get());
 }
 
 void LwpFribTable::RegisterNewStyle()
@@ -103,11 +103,11 @@ void LwpFribTable::XFConvert(XFContentContainer* pCont)
     if (!pSuper)
         return;
     sal_uInt8 nType = pSuper->GetRelativeType();
-    LwpVirtualLayout* pContainer = pSuper->GetContainerLayout();
-    if (!pContainer)
+    rtl::Reference<LwpVirtualLayout> xContainer(pSuper->GetContainerLayout());
+    if (!xContainer.is())
         return;
     if ( LwpLayoutRelativityGuts::LAY_INLINE_NEWLINE == nType
-        && !pContainer->IsCell())
+        && !xContainer->IsCell())
     {
         pXFContentContainer = m_pPara->GetXFContainer();
         //delete the additional blank para, 06/28/2005
@@ -124,11 +124,11 @@ void LwpFribTable::XFConvert(XFContentContainer* pCont)
     else if( LwpLayoutRelativityGuts::LAY_PARA_RELATIVE == nType)
     {
         //same page as text and in frame
-        if(pContainer->IsFrame())
+        if (xContainer->IsFrame())
         {
             pXFContentContainer = m_pPara->GetXFContainer();
         }
-        else if(pContainer->IsCell())
+        else if (xContainer->IsCell())
         {
             //same page as text and in cell, get the first xfpara
             rtl::Reference<XFContent> first(
diff --git a/lotuswordpro/source/filter/lwpgrfobj.cxx b/lotuswordpro/source/filter/lwpgrfobj.cxx
index 6192263..29888eb 100644
--- a/lotuswordpro/source/filter/lwpgrfobj.cxx
+++ b/lotuswordpro/source/filter/lwpgrfobj.cxx
@@ -203,7 +203,7 @@ void LwpGraphicObject::XFConvert (XFContentContainer* pCont)
             pCont->Add(iter->get());
         }
     }
-    else if (this->IsGrafFormatValid())
+    else if (this->IsGrafFormatValid() && !m_vXFDrawObjects.empty())
     {
         XFImage* pImage = static_cast<XFImage*>(m_vXFDrawObjects.front().get());
 
@@ -277,8 +277,8 @@ void LwpGraphicObject::RegisterStyle()
 
     if (m_sServerContextFormat[1]=='l'&&m_sServerContextFormat[2]=='c'&&m_sServerContextFormat[3]=='h')
     {
-        LwpVirtualLayout* pMyLayout = GetLayout(NULL);
-        if (pMyLayout && pMyLayout->IsFrame())
+        rtl::Reference<LwpVirtualLayout> xMyLayout(GetLayout(nullptr));
+        if (xMyLayout.is() && xMyLayout->IsFrame())
         {
             XFFrameStyle* pXFFrameStyle = new XFFrameStyle();
             pXFFrameStyle->SetXPosType(enumXFFrameXPosFromLeft, enumXFFrameXRelFrame);
diff --git a/lotuswordpro/source/filter/lwplayout.cxx b/lotuswordpro/source/filter/lwplayout.cxx
index 871e886..b2022c1 100644
--- a/lotuswordpro/source/filter/lwplayout.cxx
+++ b/lotuswordpro/source/filter/lwplayout.cxx
@@ -78,6 +78,9 @@
 
 LwpVirtualLayout::LwpVirtualLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
     : LwpDLNFPVList(objHdr, pStrm)
+    , m_bGettingHonorProtection(false)
+    , m_bGettingHasProtection(false)
+    , m_bGettingIsProtected(false)
     , m_nAttributes(0)
     , m_nAttributes2(0)
     , m_nAttributes3(0)
@@ -143,21 +146,17 @@ bool LwpVirtualLayout::HonorProtection()
     if(!(m_nAttributes2 & STYLE2_HONORPROTECTION))
         return false;
 
-    LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-    if(pParent && !pParent->IsHeader())
+    rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+    if (xParent.is() && !xParent->IsHeader())
     {
-        return pParent->HonorProtection();
+        return xParent->GetHonorProtection();
     }
 
     if(m_pFoundry)//is null now
     {
         LwpDocument* pDoc = m_pFoundry->GetDocument();
-        /*if(pDoc)
-        {
-            return pDoc->HonorProtection();
-        }*/
         if(pDoc && pDoc->GetRootDocument())
-            return pDoc->GetRootDocument()->HonorProtection();
+            return pDoc->GetRootDocument()->GetHonorProtection();
     }
 
     return true;
@@ -171,10 +170,10 @@ bool LwpVirtualLayout::IsProtected()
 {
     bool bProtected = (m_nAttributes & STYLE_PROTECTED)!=0;
 
-    LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-    if(pParent && !pParent->IsHeader())
+    rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+    if (xParent.is() && !xParent->IsHeader())
     {
-        if(pParent->HonorProtection()&&(pParent->HasProtection()||bProtected))
+        if (xParent->GetHonorProtection() && (xParent->GetHasProtection()||bProtected))
         {
             return true;
         }
@@ -184,7 +183,7 @@ bool LwpVirtualLayout::IsProtected()
         LwpDocument* pDoc = m_pFoundry->GetDocument();
         if(pDoc)
         {
-            if (pDoc->HonorProtection() && bProtected)
+            if (pDoc->GetHonorProtection() && bProtected)
             {
                 return true;
             }
@@ -203,10 +202,10 @@ bool LwpVirtualLayout::HasProtection()
     if(m_nAttributes & STYLE_PROTECTED)
         return true;
 
-    LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-    if(pParent && !pParent->IsHeader())
+    rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+    if (xParent.is() && !xParent->IsHeader())
     {
-        return pParent->HasProtection();
+        return xParent->GetHasProtection();
     }
 
     return false;
@@ -234,9 +233,9 @@ LwpUseWhen* LwpVirtualLayout::GetUseWhen()
     if(GetLayoutType()!=LWP_PAGE_LAYOUT)
     {
         //get parent
-        LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-        if(pParent && !pParent->IsHeader()&& (pParent->GetLayoutType()!=LWP_PAGE_LAYOUT))
-            return pParent->GetUseWhen();
+        rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+        if (xParent.is() && !xParent->IsHeader() && (xParent->GetLayoutType() != LWP_PAGE_LAYOUT))
+            return xParent->GetUseWhen();
 
     }
 
@@ -344,9 +343,9 @@ bool LwpVirtualLayout::IsMinimumHeight()
 * @descr:   Get parent layout
 *
 */
-LwpVirtualLayout* LwpVirtualLayout::GetParentLayout()
+rtl::Reference<LwpVirtualLayout> LwpVirtualLayout::GetParentLayout()
 {
-    return dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
+    return rtl::Reference<LwpVirtualLayout>(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
 }
 
 /**
@@ -356,12 +355,12 @@ LwpVirtualLayout* LwpVirtualLayout::GetParentLayout()
 void LwpVirtualLayout::RegisterChildStyle()
 {
     //Register all children styles
-    LwpVirtualLayout* pLayout = dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get());
-    while(pLayout)
+    rtl::Reference<LwpVirtualLayout> xLayout(dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get()));
+    while (xLayout.is())
     {
-        pLayout->SetFoundry(m_pFoundry);
-        pLayout->RegisterStyle();
-        pLayout = dynamic_cast<LwpVirtualLayout*>(pLayout->GetNext().obj().get());
+        xLayout->SetFoundry(m_pFoundry);
+        xLayout->RegisterStyle();
+        xLayout.set(dynamic_cast<LwpVirtualLayout*>(xLayout->GetNext().obj().get()));
     }
 }
 
@@ -375,9 +374,9 @@ bool LwpVirtualLayout::IsStyleLayout()
     if (m_nAttributes3 & STYLE3_STYLELAYOUT)
         return true;
 
-    LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get());
-    if (pParent)
-        return pParent->IsStyleLayout();
+    rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+    if (xParent.is())
+        return xParent->IsStyleLayout();
     return false;
 }
 
@@ -445,42 +444,40 @@ void LwpAssociatedLayouts::Read(LwpObjectStream* pStrm)
 * @descr:   Looking for the layout which follows the pStartLayout
 * @param:   pStartLayout - the layout which is used for looking for its following layout
 */
-LwpVirtualLayout* LwpAssociatedLayouts::GetLayout(LwpVirtualLayout *pStartLayout)
+rtl::Reference<LwpVirtualLayout> LwpAssociatedLayouts::GetLayout(LwpVirtualLayout *pStartLayout)
 {
     if (!pStartLayout && !m_OnlyLayout.IsNull())
         /* Looking for the first layout and there's only one layout in  the list.*/
-        return dynamic_cast<LwpVirtualLayout*>(m_OnlyLayout.obj().get());
+        return rtl::Reference<LwpVirtualLayout>(dynamic_cast<LwpVirtualLayout*>(m_OnlyLayout.obj().get()));
 
-    LwpObjectHolder* pObjHolder = dynamic_cast<LwpObjectHolder*>(m_Layouts.GetHead().obj().get());
-    if(pObjHolder)
+    rtl::Reference<LwpObjectHolder> xObjHolder(dynamic_cast<LwpObjectHolder*>(m_Layouts.GetHead().obj().get()));
+    if (xObjHolder.is())
     {
-        LwpVirtualLayout* pLayout = dynamic_cast<LwpVirtualLayout*>(pObjHolder->GetObject().obj().get());
-        if(!pStartLayout )
-            return pLayout;
+        rtl::Reference<LwpVirtualLayout> xLayout(dynamic_cast<LwpVirtualLayout*>(xObjHolder->GetObject().obj().get()));
+        if (!pStartLayout)
+            return xLayout;
 
-        while(pObjHolder && pStartLayout != pLayout)
+        while (xObjHolder.is() && pStartLayout != xLayout.get())
         {
-            pObjHolder = dynamic_cast<LwpObjectHolder*>(pObjHolder->GetNext().obj().get());
-            if(pObjHolder)
+            xObjHolder.set(dynamic_cast<LwpObjectHolder*>(xObjHolder->GetNext().obj().get()));
+            if (xObjHolder.is())
             {
-                pLayout = dynamic_cast<LwpVirtualLayout*>(pObjHolder->GetObject().obj().get());
+                xLayout.set(dynamic_cast<LwpVirtualLayout*>(xObjHolder->GetObject().obj().get()));
             }
         }
 
-        if(pObjHolder)
+        if (xObjHolder.is())
         {
-            pObjHolder = dynamic_cast<LwpObjectHolder*>(pObjHolder->GetNext().obj().get());
-            if(pObjHolder)
+            xObjHolder.set(dynamic_cast<LwpObjectHolder*>(xObjHolder->GetNext().obj().get()));
+            if (xObjHolder.is())
             {
-                pLayout = dynamic_cast<LwpVirtualLayout*>(pObjHolder->GetObject().obj().get());
-                return pLayout;
+                xLayout.set(dynamic_cast<LwpVirtualLayout*>(xObjHolder->GetObject().obj().get()));
+                return xLayout;
             }
         }
-
-        //return pLayout;
     }
 
-    return NULL;
+    return rtl::Reference<LwpVirtualLayout>();
 }
 
 LwpHeadLayout::LwpHeadLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm)
@@ -499,27 +496,27 @@ void LwpHeadLayout::Read()
 void LwpHeadLayout::RegisterStyle()
 {
     //Register all children styles
-    LwpVirtualLayout* pLayout = dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get());
-    while(pLayout)
+    rtl::Reference<LwpVirtualLayout> xLayout(dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get()));
+    while (xLayout.is())
     {
-        pLayout->SetFoundry(m_pFoundry);
+        xLayout->SetFoundry(m_pFoundry);
         //if the layout is relative to para, the layout will be registered in para
-        if(!pLayout->IsRelativeAnchored())
+        if (!xLayout->IsRelativeAnchored())
         {
-            if (pLayout == this)
+            if (xLayout.get() == this)
             {
                 OSL_FAIL("Layout points to itself");
                 break;
             }
-            pLayout->DoRegisterStyle();
+            xLayout->DoRegisterStyle();
         }
-        LwpVirtualLayout *pNext = dynamic_cast<LwpVirtualLayout*>(pLayout->GetNext().obj().get());
-        if (pNext == pLayout)
+        rtl::Reference<LwpVirtualLayout> xNext(dynamic_cast<LwpVirtualLayout*>(xLayout->GetNext().obj().get()));
+        if (xNext.get() == xLayout.get())
         {
             OSL_FAIL("Layout points to itself");
             break;
         }
-        pLayout = pNext;
+        xLayout = xNext;
     }
 }
 
@@ -527,18 +524,18 @@ void LwpHeadLayout::RegisterStyle()
  * @descr   find endnote supertable layout from the child layout list. Suppose that there is only one endnote supertablelayout in one division
  * @return pointer to endnote supertable layout
  */
-LwpVirtualLayout* LwpHeadLayout::FindEnSuperTableLayout()
+rtl::Reference<LwpVirtualLayout> LwpHeadLayout::FindEnSuperTableLayout()
 {
-    LwpVirtualLayout* pLayout = dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get());
-    while(pLayout)
+    rtl::Reference<LwpVirtualLayout> xLayout(dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get()));
+    while (xLayout.get())
     {
-        if(pLayout->GetLayoutType() == LWP_ENDNOTE_SUPERTABLE_LAYOUT)
+        if (xLayout->GetLayoutType() == LWP_ENDNOTE_SUPERTABLE_LAYOUT)
         {
-            return pLayout;
+            return xLayout;
         }
-        pLayout = dynamic_cast<LwpVirtualLayout*>(pLayout->GetNext().obj().get());
+        xLayout.set(dynamic_cast<LwpVirtualLayout*>(xLayout->GetNext().obj().get()));
     }
-    return NULL;
+    return rtl::Reference<LwpVirtualLayout>();
 }
 
 LwpLayoutStyle::LwpLayoutStyle()
@@ -671,9 +668,13 @@ LwpLayoutGeometry* LwpMiddleLayout::GetGeometry()
     {
         return ( dynamic_cast<LwpLayoutGeometry*> (m_LayGeometry.obj().get()) );
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetGeometry();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetGeometry();
+        }
     }
     return NULL;
 }
@@ -718,12 +719,12 @@ bool LwpMiddleLayout::MarginsSameAsParent()
     {
         return LwpVirtualLayout::MarginsSameAsParent();
     }
-    if (LwpVirtualLayout* pLay = dynamic_cast<LwpVirtualLayout*> (GetBasedOnStyle().get()))
+    rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+    if (LwpVirtualLayout* pLay = dynamic_cast<LwpVirtualLayout*>(xBase.get()))
     {
         pLay->MarginsSameAsParent();
     }
     return LwpVirtualLayout::MarginsSameAsParent();
-
 }
 
 /**
@@ -737,10 +738,10 @@ double LwpMiddleLayout::GetMarginsValue(const sal_uInt8 &nWhichSide)
     {
         if ( MarginsSameAsParent() )
         {
-            LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-            if(pParent && !pParent->IsHeader())
+            rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+            if (xParent.is() && !xParent->IsHeader())
             {
-                fValue = pParent->GetMarginsValue(nWhichSide);
+                fValue = xParent->GetMarginsValue(nWhichSide);
                 return fValue;
             }
         }
@@ -755,8 +756,9 @@ double LwpMiddleLayout::GetMarginsValue(const sal_uInt8 &nWhichSide)
             return fValue;
         }
     }
-    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(GetBasedOnStyle().get());
-    if(pStyle)
+    rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(xBase.get());
+    if (pStyle)
     {
         fValue = pStyle->GetMarginsValue(nWhichSide);
         return fValue;
@@ -802,9 +804,13 @@ LwpBorderStuff* LwpMiddleLayout::GetBorderStuff()
         LwpLayoutBorder* pLayoutBorder = dynamic_cast<LwpLayoutBorder*>(m_LayBorderStuff.obj().get());
         return pLayoutBorder ? &pLayoutBorder->GetBorderStuff() : NULL;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetBorderStuff();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetBorderStuff();
+        }
     }
     return NULL;
 }
@@ -822,9 +828,13 @@ LwpBackgroundStuff* LwpMiddleLayout::GetBackgroundStuff()
         LwpLayoutBackground* pLayoutBackground = dynamic_cast<LwpLayoutBackground*>(m_LayBackgroundStuff.obj().get());
         return pLayoutBackground ? &pLayoutBackground->GetBackgoudStuff() : NULL;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetBackgroundStuff();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetBackgroundStuff();
+        }
     }
     return NULL;
 }
@@ -931,9 +941,13 @@ LwpTabOverride* LwpMiddleLayout::GetTabOverride()
         }
         return NULL;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*> ( GetBasedOnStyle().get() ))
+    else
     {
-        return pLay->GetTabOverride();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetTabOverride();
+        }
     }
     return NULL;
 }
@@ -946,68 +960,70 @@ LwpTabOverride* LwpMiddleLayout::GetTabOverride()
 */
 sal_uInt16 LwpMiddleLayout::GetScaleMode()
 {
-    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is())
+    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is() && GetLayoutScale())
         return GetLayoutScale()->GetScaleMode();
     rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
     if (xBase.is())
-        return dynamic_cast<LwpMiddleLayout*>(xBase.get())->GetScaleMode();
+        return dynamic_cast<LwpMiddleLayout&>(*xBase.get()).GetScaleMode();
     else
         return (LwpLayoutScale::FIT_IN_FRAME | LwpLayoutScale::MAINTAIN_ASPECT_RATIO);
 }
 
 sal_uInt16 LwpMiddleLayout::GetScaleTile()
 {
-    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is())
+    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is() && GetLayoutScale())
         return (GetLayoutScale()->GetPlacement() & LwpLayoutScale::TILED)
             ? 1 : 0;
     rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
     if (xBase.is())
-        return dynamic_cast<LwpMiddleLayout*>(xBase.get())->GetScaleTile();
+        return dynamic_cast<LwpMiddleLayout&>(*xBase.get()).GetScaleTile();
     else
         return 0;
 }
 
 sal_uInt16 LwpMiddleLayout::GetScaleCenter()
 {
-    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is())
+    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is() && GetLayoutScale())
+    {
         return (GetLayoutScale()->GetPlacement() & LwpLayoutScale::CENTERED)
             ? 1 : 0;
+    }
     rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
     if (xBase.is())
-        return dynamic_cast<LwpMiddleLayout*>(xBase.get())->GetScaleCenter();
+        return dynamic_cast<LwpMiddleLayout&>(*xBase.get()).GetScaleCenter();
     else
         return 0;
 }
 
 sal_uInt32 LwpMiddleLayout::GetScalePercentage()
 {
-    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is())
+    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is() && GetLayoutScale())
         return GetLayoutScale()->GetScalePercentage()/10;//m_nScalePercentage 1000 = 100%
     rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
     if (xBase.is())
-        return dynamic_cast<LwpMiddleLayout*>(xBase.get())->GetScalePercentage();
+        return dynamic_cast<LwpMiddleLayout&>(*xBase.get()).GetScalePercentage();
     else
         return 100;
 }
 
 double LwpMiddleLayout::GetScaleWidth()
 {
-    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is())
+    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is() && GetLayoutScale())
         return LwpTools::ConvertFromUnits(GetLayoutScale()->GetScaleWidth());
     rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
     if (xBase.is())
-        return dynamic_cast<LwpMiddleLayout*>(xBase.get())->GetScaleWidth();
+        return dynamic_cast<LwpMiddleLayout&>(*xBase.get()).GetScaleWidth();
     else
         return 0;
 }
 
 double LwpMiddleLayout::GetScaleHeight()
 {
-    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is())
+    if ((m_nOverrideFlag & OVER_SCALING) && m_LayScale.obj().is() && GetLayoutScale())
         return LwpTools::ConvertFromUnits(GetLayoutScale()->GetScaleHeight());
     rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
     if (xBase.is())
-        return dynamic_cast<LwpMiddleLayout*>(xBase.get())->GetScaleHeight();
+        return dynamic_cast<LwpMiddleLayout&>(*xBase.get()).GetScaleHeight();
     else
         return 0;
 }
@@ -1175,9 +1191,13 @@ bool LwpMiddleLayout::IsAutoGrow()
             ((LAY_AUTOGROW << SHIFT_UP) | (LAY_AUTOGROW << SHIFT_DOWN) |
             (LAY_AUTOGROW << SHIFT_RIGHT) | (LAY_AUTOGROW << SHIFT_LEFT))) != 0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsAutoGrow();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->IsAutoGrow();
+        }
     }
     return LwpVirtualLayout::IsAutoGrow();
 }
@@ -1192,9 +1212,13 @@ bool LwpMiddleLayout::IsAutoGrowDown()
     {
         return (m_nDirection & (LAY_AUTOGROW << SHIFT_DOWN)) != 0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsAutoGrowDown();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->IsAutoGrowDown();
+        }
     }
     return LwpVirtualLayout::IsAutoGrowDown();
 }
@@ -1209,9 +1233,13 @@ bool LwpMiddleLayout::IsAutoGrowUp()
     {
         return (m_nDirection & (LAY_AUTOGROW << SHIFT_UP)) != 0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsAutoGrowUp();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->IsAutoGrowUp();
+        }
     }
     return LwpVirtualLayout::IsAutoGrowUp();
 }
@@ -1226,9 +1254,13 @@ bool LwpMiddleLayout::IsAutoGrowLeft()
     {
         return (m_nDirection & (LAY_AUTOGROW << SHIFT_LEFT)) != 0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsAutoGrowLeft();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->IsAutoGrowLeft();
+        }
     }
     return LwpVirtualLayout::IsAutoGrowLeft();
 }
@@ -1243,9 +1275,13 @@ bool LwpMiddleLayout::IsAutoGrowRight()
     {
         return (m_nDirection & (LAY_AUTOGROW << SHIFT_RIGHT)) != 0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*> (GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsAutoGrowRight();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->IsAutoGrowRight();
+        }
     }
     return LwpVirtualLayout::IsAutoGrowRight();
 }
@@ -1263,9 +1299,13 @@ sal_uInt8 LwpMiddleLayout::GetContentOrientation()
         if (pLayGeometry)
             return pLayGeometry->GetContentOrientation();
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetContentOrientation();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetContentOrientation();
+        }
     }
     return LwpVirtualLayout::GetContentOrientation();
 }
@@ -1281,24 +1321,28 @@ bool LwpMiddleLayout::HonorProtection()
         if(!(m_nAttributes2 & STYLE2_HONORPROTECTION))
             return false;
 
-        LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-        if(pParent && !pParent->IsHeader())
+        rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+        if (xParent.is() && !xParent->IsHeader())
         {
-            return pParent->HonorProtection();
+            return xParent->GetHonorProtection();
         }
 
         if(m_pFoundry)//is null now
         {
             LwpDocument* pDoc = m_pFoundry->GetDocument();
-            if(pDoc)
+            if (pDoc)
             {
-                return pDoc->HonorProtection();
+                return pDoc->GetHonorProtection();
             }
         }
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*> (GetBasedOnStyle().get()))
+    else
     {
-        return pLay->HonorProtection();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetHonorProtection();
+        }
     }
 
     return LwpVirtualLayout::HonorProtection();
@@ -1315,21 +1359,25 @@ bool LwpMiddleLayout::IsProtected()
     {
         bProtected = (m_nAttributes & STYLE_PROTECTED)!=0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*> (GetBasedOnStyle().get()))
+    else
     {
-        bProtected = pLay->IsProtected();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            bProtected = pLay->GetIsProtected();
+        }
+        else
+            bProtected = LwpVirtualLayout::IsProtected();
     }
-    else
-        bProtected = LwpVirtualLayout::IsProtected();
 
-    LwpVirtualLayout* pParent = dynamic_cast<LwpVirtualLayout*> (GetParent().obj().get());
-    if(pParent && !pParent->IsHeader())
+    rtl::Reference<LwpVirtualLayout> xParent(dynamic_cast<LwpVirtualLayout*>(GetParent().obj().get()));
+    if (xParent.is() && !xParent->IsHeader())
     {
         /* If a parent's protected then none of its children can be accessed. */
-        if(pParent->IsProtected())
+        if (xParent->GetIsProtected())
             return true;
 
-        if(pParent->HonorProtection())
+        if (xParent->GetHonorProtection())
             return bProtected;
 
         /* If our parent isn't honoring protection then we aren't protected. */
@@ -1341,7 +1389,7 @@ bool LwpMiddleLayout::IsProtected()
         LwpDocument* pDoc = m_pFoundry->GetDocument();
         if(pDoc)
         {
-            if (pDoc->HonorProtection())
+            if (pDoc->GetHonorProtection())
                 return bProtected;
 
             /* If the document isn't honoring protection then we aren't protected.*/
@@ -1356,18 +1404,18 @@ bool LwpMiddleLayout::IsProtected()
 * @descr:   Get watermark layout
 *
 */
-LwpVirtualLayout* LwpMiddleLayout::GetWaterMarkLayout()
+rtl::Reference<LwpVirtualLayout> LwpMiddleLayout::GetWaterMarkLayout()
 {
-    LwpVirtualLayout* pLay = dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get());
-    while(pLay)
+    rtl::Reference<LwpVirtualLayout> xLay(dynamic_cast<LwpVirtualLayout*>(GetChildHead().obj().get()));
+    while (xLay.is())
     {
-        if( pLay->IsForWaterMark())
+        if (xLay->IsForWaterMark())
         {
-            return pLay;
+            return xLay;
         }
-        pLay = dynamic_cast<LwpVirtualLayout*> (pLay->GetNext().obj().get());
+        xLay.set(dynamic_cast<LwpVirtualLayout*>(xLay->GetNext().obj().get()));
     }
-    return NULL;
+    return rtl::Reference<LwpVirtualLayout>();
 }
 
 /**
@@ -1376,7 +1424,8 @@ LwpVirtualLayout* LwpMiddleLayout::GetWaterMarkLayout()
 */
 XFBGImage* LwpMiddleLayout::GetXFBGImage()
 {
-    LwpMiddleLayout* pLay = static_cast<LwpMiddleLayout*>(GetWaterMarkLayout());
+    rtl::Reference<LwpVirtualLayout> xWaterMarkLayout(GetWaterMarkLayout());
+    LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xWaterMarkLayout.get());
     if(pLay)
     {
         //test BGImage
@@ -1440,9 +1489,13 @@ bool LwpMiddleLayout::GetUsePrinterSettings()
     {
         return (m_nAttributes3 & STYLE3_USEPRINTERSETTINGS) != 0;
     }
-    else if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*> (GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetUsePrinterSettings();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpMiddleLayout* pLay = dynamic_cast<LwpMiddleLayout*>(xBase.get()))
+        {
+            return pLay->GetUsePrinterSettings();
+        }
     }
     return false;
 }
@@ -1519,7 +1572,8 @@ sal_uInt16 LwpLayout::GetNumCols()
         }
     }
 
-    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*> (GetBasedOnStyle().get());
+    rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(xBase.get());
     if (pStyle)
     {
         return pStyle->GetNumCols();
@@ -1544,7 +1598,8 @@ double LwpLayout::GetColWidth(sal_uInt16 nIndex)
         }
     }
 
-    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(GetBasedOnStyle().get());
+    rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(xBase.get());
     if (pStyle)
     {
         return pStyle->GetColWidth(nIndex);
@@ -1569,7 +1624,8 @@ double LwpLayout::GetColGap(sal_uInt16 nIndex)
         }
     }
 
-    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(GetBasedOnStyle().get());
+    rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+    LwpVirtualLayout* pStyle = dynamic_cast<LwpVirtualLayout*>(xBase.get());
     if (pStyle)
     {
         return pStyle->GetColGap(nIndex);
@@ -1714,9 +1770,13 @@ sal_uInt16 LwpLayout::GetUsePage()
         else
             return 0;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetUsePage();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->GetUsePage();
+        }
     }
     return 0;
 }
@@ -1731,9 +1791,13 @@ LwpUseWhen* LwpLayout::VirtualGetUseWhen()
     {
         return m_pUseWhen;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->VirtualGetUseWhen();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->VirtualGetUseWhen();
+        }
     }
     return LwpVirtualLayout::VirtualGetUseWhen();
 }
@@ -1752,9 +1816,13 @@ bool LwpLayout::IsUseOnAllPages()
         else
             return false;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsUseOnAllPages();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->IsUseOnAllPages();
+        }
     }
     return LwpVirtualLayout::IsUseOnAllPages();
 }
@@ -1773,9 +1841,13 @@ bool LwpLayout::IsUseOnAllEvenPages()
         else
             return false;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsUseOnAllEvenPages();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->IsUseOnAllEvenPages();
+        }
     }
     return LwpVirtualLayout::IsUseOnAllEvenPages();
 }
@@ -1794,9 +1866,13 @@ bool LwpLayout::IsUseOnAllOddPages()
         else
             return false;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsUseOnAllOddPages();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->IsUseOnAllOddPages();
+        }
     }
     return LwpVirtualLayout::IsUseOnAllOddPages();
 }
@@ -1815,9 +1891,13 @@ bool LwpLayout::IsUseOnPage()
         else
             return false;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->IsUseOnPage();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->IsUseOnPage();
+        }
     }
     return LwpVirtualLayout::IsUseOnPage();
 }
@@ -1835,9 +1915,13 @@ LwpShadow* LwpLayout::GetShadow()
         LwpLayoutShadow* pLayoutShadow = dynamic_cast<LwpLayoutShadow*>(m_LayShadow.obj().get());
         return pLayoutShadow ? &pLayoutShadow->GetShadow() : NULL;
     }
-    else if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetShadow();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpLayout* pLay = dynamic_cast<LwpLayout*>(xBase.get()))
+        {
+            return pLay->GetShadow();
+        }
     }
     return NULL;
 }
@@ -1900,7 +1984,7 @@ XFShadow* LwpLayout::GetXFShadow()
  * @descr get the layout that containers the current frame layout
  *
  */
-LwpVirtualLayout* LwpLayout::GetContainerLayout()
+rtl::Reference<LwpVirtualLayout> LwpLayout::GetContainerLayout()
 {
     if(IsRelativeAnchored())
     {
@@ -1981,9 +2065,13 @@ sal_uInt8 LwpPlacableLayout::GetWrapType()
     {
         return m_nWrapType;
     }
-    else if (LwpPlacableLayout* pLay = dynamic_cast<LwpPlacableLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetWrapType();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpPlacableLayout* pLay = dynamic_cast<LwpPlacableLayout*>(xBase.get()))
+        {
+            return pLay->GetWrapType();
+        }
     }
     return LAY_WRAP_AROUND;
 }
@@ -2002,9 +2090,13 @@ LwpLayoutRelativity* LwpPlacableLayout::GetRelativityPiece()
             return dynamic_cast<LwpLayoutRelativity*>(m_LayRelativity.obj().get());
         }
     }
-    else if (LwpPlacableLayout* pLay = dynamic_cast<LwpPlacableLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetRelativityPiece();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpPlacableLayout* pLay = dynamic_cast<LwpPlacableLayout*>(xBase.get()))
+        {
+            return pLay->GetRelativityPiece();
+        }
     }
     return NULL;
 }
@@ -2043,9 +2135,13 @@ sal_Int32 LwpPlacableLayout::GetBaseLineOffset()
     {
         return m_nBaseLineOffset;
     }
-    else if (LwpPlacableLayout* pLay = dynamic_cast<LwpPlacableLayout*>(GetBasedOnStyle().get()))
+    else
     {
-        return pLay->GetBaseLineOffset();
+        rtl::Reference<LwpObject> xBase(GetBasedOnStyle());
+        if (LwpPlacableLayout* pLay = dynamic_cast<LwpPlacableLayout*>(xBase.get()))
+        {
+            return pLay->GetBaseLineOffset();
+        }
     }
     return 0;
 
@@ -2059,9 +2155,8 @@ bool LwpPlacableLayout::IsAnchorPage()
     if(IsRelativeAnchored())
         return false;
 
-    LwpVirtualLayout* pLayout = GetParentLayout();
-    if(pLayout && (pLayout->IsPage() || pLayout->IsHeader() || pLayout->IsFooter()))
-    //if(pLayout && pLayout->IsPage())
+    rtl::Reference<LwpVirtualLayout> xLayout = GetParentLayout();
+    if (xLayout.is() && (xLayout->IsPage() || xLayout->IsHeader() || xLayout->IsFooter()))
     {
         return true;
     }
@@ -2076,8 +2171,8 @@ bool LwpPlacableLayout::IsAnchorFrame()
     if(IsRelativeAnchored())
         return false;
 
-    LwpVirtualLayout* pLayout = GetParentLayout();
-    if(pLayout && (pLayout->IsFrame()||pLayout->IsGroupHead()))
+    rtl::Reference<LwpVirtualLayout> xLayout = GetParentLayout();
+    if (xLayout.is() && (xLayout->IsFrame() || xLayout->IsGroupHead()))
     {
         return true;
     }
@@ -2092,8 +2187,8 @@ bool LwpPlacableLayout::IsAnchorCell()
     if(IsRelativeAnchored())
         return false;
 
-    LwpVirtualLayout* pLayout = GetParentLayout();
-    if(pLayout && pLayout->IsCell())
+    rtl::Reference<LwpVirtualLayout> xLayout = GetParentLayout();
+    if (xLayout.is() && xLayout->IsCell())
     {
         return true;
     }
diff --git a/lotuswordpro/source/filter/lwplayout.hxx b/lotuswordpro/source/filter/lwplayout.hxx
index 5e9b42e..a7bcf1e 100644
--- a/lotuswordpro/source/filter/lwplayout.hxx
+++ b/lotuswordpro/source/filter/lwplayout.hxx
@@ -115,9 +115,33 @@ public:
     bool IsAutoGrowWidth();
     bool IsInlineToMargin();
     virtual sal_uInt8 GetContentOrientation(){ return TEXT_ORIENT_LRTB;}
-    virtual bool HonorProtection();
-    virtual bool IsProtected();
-    bool HasProtection();
+    bool GetHonorProtection()
+    {
+        if (m_bGettingHonorProtection)
+            throw std::runtime_error("recursion in layout");
+        m_bGettingHonorProtection = true;
+        bool bRet = HonorProtection();
+        m_bGettingHonorProtection = false;
+        return bRet;
+    }
+    bool GetIsProtected()
+    {
+        if (m_bGettingIsProtected)
+            throw std::runtime_error("recursion in layout");
+        m_bGettingIsProtected = true;
+        bool bRet = IsProtected();
+        m_bGettingIsProtected = false;
+        return bRet;
+    }
+    bool GetHasProtection()
+    {
+        if (m_bGettingHasProtection)
+            throw std::runtime_error("recursion in layout");
+        m_bGettingHasProtection = true;
+        bool bRet = HasProtection();
+        m_bGettingHasProtection = false;
+        return bRet;
+    }
     OUString GetStyleName(){ return m_StyleName;}
     bool IsComplex();
     virtual bool IsAnchorPage(){ return false;}
@@ -141,9 +165,9 @@ public:
     virtual sal_Int32 GetPageNumber(sal_uInt16 /*nLayoutNumber*/ = 0){ return -1;}
     bool IsMinimumHeight();
     virtual bool IsForWaterMark(){ return false;}
-    virtual LwpPara* GetLastParaOfPreviousStory() { return NULL; }
-    LwpVirtualLayout* GetParentLayout();
-    virtual LwpVirtualLayout* GetContainerLayout(){ return NULL;}
+    virtual LwpPara* GetLastParaOfPreviousStory() { return nullptr; }
+    rtl::Reference<LwpVirtualLayout> GetParentLayout();
+    virtual rtl::Reference<LwpVirtualLayout> GetContainerLayout() { return rtl::Reference<LwpVirtualLayout>(); }
     void RegisterChildStyle();
     bool NoContentReference();
     bool IsStyleLayout();
@@ -167,7 +191,13 @@ public:
     //End by
 protected:
     void Read() SAL_OVERRIDE;
+    bool HasProtection();
+    virtual bool HonorProtection();
+    virtual bool IsProtected();
 protected:
+    bool m_bGettingHonorProtection;
+    bool m_bGettingHasProtection;
+    bool m_bGettingIsProtected;
     sal_uInt32 m_nAttributes;
     sal_uInt32 m_nAttributes2;
     sal_uInt32 m_nAttributes3;
@@ -229,8 +259,7 @@ public:
 public:
     void Read(LwpObjectStream* pStrm);
     LwpObjectID& GetOnlyLayout() { return m_OnlyLayout;}
-    LwpDLVListHeadTail& GetLayouts() { return m_Layouts;}
-    LwpVirtualLayout* GetLayout(LwpVirtualLayout* pStartLayout);
+    rtl::Reference<LwpVirtualLayout> GetLayout(LwpVirtualLayout* pStartLayout);
 protected:
     LwpObjectID m_OnlyLayout; //LwpVirtualLayout
     LwpDLVListHeadTail m_Layouts;
@@ -242,7 +271,7 @@ public:
     LwpHeadLayout(LwpObjectHeader &objHdr, LwpSvStream* pStrm);
     virtual ~LwpHeadLayout(){}
     void RegisterStyle() SAL_OVERRIDE;
-    LwpVirtualLayout* FindEnSuperTableLayout();
+    rtl::Reference<LwpVirtualLayout> FindEnSuperTableLayout();
 protected:
     void Read() SAL_OVERRIDE;
     virtual LWP_LAYOUT_TYPE GetLayoutType () SAL_OVERRIDE { return LWP_HEAD_LAYOUT;}
@@ -298,7 +327,7 @@ public:
     virtual sal_uInt8 GetContentOrientation() SAL_OVERRIDE;
     virtual bool HonorProtection() SAL_OVERRIDE;
     virtual bool IsProtected() SAL_OVERRIDE;
-    LwpVirtualLayout* GetWaterMarkLayout();
+    rtl::Reference<LwpVirtualLayout> GetWaterMarkLayout();
     XFBGImage* GetXFBGImage();
     bool GetUsePrinterSettings();
 
@@ -395,7 +424,7 @@ public:
     virtual bool IsUseOnAllOddPages() SAL_OVERRIDE;
     virtual bool IsUseOnPage() SAL_OVERRIDE;
     LwpObjectID& GetPosition(){ return m_Positon;}
-    virtual LwpVirtualLayout* GetContainerLayout() SAL_OVERRIDE;
+    virtual rtl::Reference<LwpVirtualLayout> GetContainerLayout() SAL_OVERRIDE;
 };
 
 class LwpPlacableLayout : public LwpLayout
diff --git a/lotuswordpro/source/filter/lwpnotes.cxx b/lotuswordpro/source/filter/lwpnotes.cxx
index dd08b17..bd16ae9 100644
--- a/lotuswordpro/source/filter/lwpnotes.cxx
+++ b/lotuswordpro/source/filter/lwpnotes.cxx
@@ -101,7 +101,7 @@ void LwpFribNote::RegisterNewStyle()
  */
 void LwpFribNote::XFConvert(XFContentContainer* pCont)
 {
-    LwpNoteLayout* pLayout =static_cast<LwpNoteLayout*>(m_Layout.obj().get());
+    LwpNoteLayout* pLayout = dynamic_cast<LwpNoteLayout*>(m_Layout.obj().get());
     if(pLayout)
     {
         XFAnnotation* pXFNote = new XFAnnotation;
@@ -217,10 +217,10 @@ OUString LwpNoteLayout::GetAuthor()
     LwpNoteHeaderLayout* pTextLayout = static_cast<LwpNoteHeaderLayout*>(FindChildByType(LWP_NOTEHEADER_LAYOUT));
     if(pTextLayout)
     {
-        LwpStory* pStory = static_cast<LwpStory*>(pTextLayout->GetContent().obj().get());
+        LwpStory* pStory = dynamic_cast<LwpStory*>(pTextLayout->GetContent().obj().get());
         if(pStory)
         {
-            LwpPara* pFirst = static_cast<LwpPara*>(pStory->GetFirstPara().obj().get());
+            LwpPara* pFirst = dynamic_cast<LwpPara*>(pStory->GetFirstPara().obj().get());
             if(pFirst)
                 return pFirst->GetContentText(true);
         }
diff --git a/lotuswordpro/source/filter/lwpobjfactory.cxx b/lotuswordpro/source/filter/lwpobjfactory.cxx
index f73f800..5938635 100644
--- a/lotuswordpro/source/filter/lwpobjfactory.cxx
+++ b/lotuswordpro/source/filter/lwpobjfactory.cxx
@@ -670,10 +670,15 @@ rtl::Reference<LwpObject> LwpObjectFactory::CreateObject(sal_uInt32 type, LwpObj
             break;
         }
     }
-    if(newObj.is())
+    if (newObj.is())
     {
         newObj->QuickRead();
-        m_IdToObjList.insert(LwpIdToObjMap::value_type(objHdr.GetID(), newObj));
+        auto result = m_IdToObjList.insert(LwpIdToObjMap::value_type(objHdr.GetID(), newObj));
+        if (!result.second)
+        {
+            SAL_WARN("lwp", "clearing duplicate object");

... etc. - the rest is truncated


More information about the Libreoffice-commits mailing list