lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] PATCH: Fix memory leak in BasicValues constructor.


From: Vadim Zeitlin
Subject: [lmi] PATCH: Fix memory leak in BasicValues constructor.
Date: Fri, 22 Aug 2014 02:31:43 +0200

 Hello,

 I've noticed a relatively big memory leak during testing and it turns out
that it comes from the "kludge_input" in BasicValues ctor which is simply
allocated and never deleted currently. I realize that the name of this
variable clearly indicates that it's planned for replacement with something
else in the future anyhow, but in the meanwhile it would be nice if you
could please apply this patch if only to avoid leak reports on program exit
which could hide other, worse, problems (but also because this is quite a
real memory leak and I guess its effects could, in theory, become
noticeable if the program were running for long enough and enough GPTs were
created).

 I've examined the code to check for the possibility that this kludge_input
object actually needs to remain alive after being copied, but to the best
of my knowledge, this is not the case and everything continues to work in
the same way as before with this change (e.g. the same GPT file is created)
and dynamic analysis tools don't report any new problems. If I missed
something and we really need to keep this object alive, I'd like to at
least apply a kludge to this kludge and keep all these pointers in a static
container which would be emptied before the program exit, so as to at least
avoid the memory leak reports, if not to fix the real memory leak problem.
Please let me know if I should redo the patch like this.

 TIA!
VZ

--- cut here ---
>From cee665627b495e624bb8a12d5fb7464aee1815d1 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <address@hidden>
Date: Fri, 22 Aug 2014 02:23:17 +0200
Subject: Fix memory leak in BasicValues constructor.

Don't leak the heap-allocated kludge_input object, allocate it on the stack
instead.

diff --git a/ihs_basicval.cpp b/ihs_basicval.cpp
index efda1fb..c1d3fd6 100644
--- a/ihs_basicval.cpp
+++ b/ihs_basicval.cpp
@@ -117,38 +117,38 @@ std::string mc_str(T t)
     ,PremiumTaxState_    (a_StateOfJurisdiction)
     ,InitialTargetPremium(a_TargetPremium)
 {
-    Input* kludge_input = new Input;
+    Input kludge_input;
 
-    (*kludge_input)["IssueAge"         ] = 
value_cast<std::string>(a_IssueAge);    yare_input_.IssueAge                   
= a_IssueAge           ;
-    (*kludge_input)["RetirementAge"    ] = 
value_cast<std::string>(a_IssueAge);    yare_input_.RetirementAge              
= a_IssueAge           ;
-    (*kludge_input)["Gender"           ] = mc_str(a_Gender)                   
;    yare_input_.Gender                     = a_Gender             ;
-    (*kludge_input)["Smoking"          ] = mc_str(a_Smoker)                   
;    yare_input_.Smoking                    = a_Smoker             ;
-    (*kludge_input)["UnderwritingClass"] = mc_str(a_UnderwritingClass)        
;    yare_input_.UnderwritingClass          = a_UnderwritingClass  ;
+    kludge_input["IssueAge"         ] = value_cast<std::string>(a_IssueAge);   
 yare_input_.IssueAge                   = a_IssueAge           ;
+    kludge_input["RetirementAge"    ] = value_cast<std::string>(a_IssueAge);   
 yare_input_.RetirementAge              = a_IssueAge           ;
+    kludge_input["Gender"           ] = mc_str(a_Gender)                   ;   
 yare_input_.Gender                     = a_Gender             ;
+    kludge_input["Smoking"          ] = mc_str(a_Smoker)                   ;   
 yare_input_.Smoking                    = a_Smoker             ;
+    kludge_input["UnderwritingClass"] = mc_str(a_UnderwritingClass)        ;   
 yare_input_.UnderwritingClass          = a_UnderwritingClass  ;
     if(a_AdbInForce)
         {
-        (*kludge_input)["AccidentalDeathBenefit"] = "Yes";                     
    yare_input_.AccidentalDeathBenefit     = mce_yes              ;
+        kludge_input["AccidentalDeathBenefit"] = "Yes";                        
 yare_input_.AccidentalDeathBenefit     = mce_yes              ;
         }
     else
         {
-        (*kludge_input)["AccidentalDeathBenefit"] = "No";                      
    yare_input_.AccidentalDeathBenefit     = mce_no               ;
+        kludge_input["AccidentalDeathBenefit"] = "No";                         
 yare_input_.AccidentalDeathBenefit     = mce_no               ;
         }
-    (*kludge_input)["GroupUnderwritingType"     ] = 
mc_str(a_UnderwritingBasis);   yare_input_.GroupUnderwritingType      = 
a_UnderwritingBasis  ;
-    (*kludge_input)["ProductName"               ] = a_ProductName;             
    yare_input_.ProductName                = a_ProductName        ;
-    (*kludge_input)["PremiumTaxState"           ] = 
mc_str(a_StateOfJurisdiction); yare_input_.PremiumTaxState            = 
a_StateOfJurisdiction;
-    (*kludge_input)["DefinitionOfLifeInsurance" ] = "GPT";                     
    yare_input_.DefinitionOfLifeInsurance  = mce_gpt              ;
-    (*kludge_input)["DefinitionOfMaterialChange"] = "GPT adjustment event";    
    yare_input_.DefinitionOfMaterialChange = mce_adjustment_event ;
+    kludge_input["GroupUnderwritingType"     ] = mc_str(a_UnderwritingBasis);  
 yare_input_.GroupUnderwritingType      = a_UnderwritingBasis  ;
+    kludge_input["ProductName"               ] = a_ProductName;                
 yare_input_.ProductName                = a_ProductName        ;
+    kludge_input["PremiumTaxState"           ] = 
mc_str(a_StateOfJurisdiction); yare_input_.PremiumTaxState            = 
a_StateOfJurisdiction;
+    kludge_input["DefinitionOfLifeInsurance" ] = "GPT";                        
 yare_input_.DefinitionOfLifeInsurance  = mce_gpt              ;
+    kludge_input["DefinitionOfMaterialChange"] = "GPT adjustment event";       
 yare_input_.DefinitionOfMaterialChange = mce_adjustment_event ;
 
-    (*kludge_input)["SpecifiedAmount"   ] = 
value_cast<std::string>(a_FaceAmount); yare_input_.SpecifiedAmount            
.assign(1, a_FaceAmount);
+    kludge_input["SpecifiedAmount"   ] = 
value_cast<std::string>(a_FaceAmount); yare_input_.SpecifiedAmount            
.assign(1, a_FaceAmount);
 
     mce_dbopt const z
         (mce_option1_for_7702 == a_DBOptFor7702 ? mce_option1
         :mce_option2_for_7702 == a_DBOptFor7702 ? mce_option2
         :throw std::runtime_error("Unexpected DB option.")
         );
-    (*kludge_input)["DeathBenefitOption"] = mce_dbopt(z).str();                
    yare_input_.DeathBenefitOption         .assign(1, z.value());
+    kludge_input["DeathBenefitOption"] = mce_dbopt(z).str();                   
 yare_input_.DeathBenefitOption         .assign(1, z.value());
 
     // TODO ?? EGREGIOUS_DEFECT Redesign this function instead.
-    const_cast<Input&>(*Input_) = *kludge_input;
+    const_cast<Input&>(*Input_) = kludge_input;
 
     GPTServerInit();
 }
-- 
1.7.9


reply via email to

[Prev in Thread] Current Thread [Next in Thread]