Index: /issm/branches/trunk-larour-SLPS2022/src/c/analyses/SealevelchangeAnalysis.cpp
===================================================================
--- /issm/branches/trunk-larour-SLPS2022/src/c/analyses/SealevelchangeAnalysis.cpp	(revision 27103)
+++ /issm/branches/trunk-larour-SLPS2022/src/c/analyses/SealevelchangeAnalysis.cpp	(revision 27104)
@@ -9,4 +9,14 @@
 
 /*Model processing*/
+SealevelchangeAnalysis::~SealevelchangeAnalysis(){/*{{{*/
+
+	BarystaticContributions* barycontrib=NULL;
+	GenericParam<BarystaticContributions*>* barycontribparam=NULL;
+
+	barycontribparam = xDynamicCast<GenericParam<BarystaticContributions*>*>(femmodel->parameters->FindParamObject(BarystaticContributionsEnum));
+	barycontrib=barycontribparam->GetParameterValue();
+
+	delete barycontrib;
+}/*}}}*/
 void SealevelchangeAnalysis::CreateConstraints(Constraints* constraints,IoModel* iomodel){/*{{{*/
 	/*No constraints*/
@@ -43,4 +53,5 @@
 	/*external solidearthsolution: solid-Earth model*/
 	iomodel->FetchData(&isexternal,"md.solidearth.isexternal");
+
 	if(isexternal){
 		iomodel->FetchData(&horiz,"md.solidearth.settings.horiz");
@@ -209,5 +220,5 @@
 		iomodel->FetchData(&externalnature,"md.solidearth.external.nature");
 		if(externalnature>=3){
-			int modelid; 
+			IssmDouble modelid; 
 			int nummodels;
 
@@ -276,4 +287,7 @@
 			parameters->AddObject(new DoubleMatParam(LoveTimeFreqEnum,love_timefreq,precomputednt,1));
 			parameters->AddObject(new BoolParam(LoveIsTimeEnum,love_istime));
+
+			/*Free allocations:*/
+			xDelete<IssmDouble>(love_timefreq);
 
 			// AD performance is sensitive to calls to ensurecontiguous.
@@ -524,12 +538,17 @@
 				nt=1; //in elastic, or if we run only selfattraction, we need only one step
 #ifdef _HAVE_AD_
-				G_viscoelastic_interpolated=G_viscoelastic;
-				U_viscoelastic_interpolated=U_viscoelastic;
-				if(horiz)H_viscoelastic_interpolated=H_viscoelastic;
+				G_viscoelastic_interpolated=xNew<IssmDouble>(M,"t");
+				U_viscoelastic_interpolated=xNew<IssmDouble>(M,"t");
+				if(horiz) H_viscoelastic_interpolated=xNew<IssmDouble>(M,"t");
 #else
-				G_viscoelastic_interpolated=G_viscoelastic;
-				U_viscoelastic_interpolated=U_viscoelastic;
-				if(horiz)H_viscoelastic_interpolated=H_viscoelastic;
+				G_viscoelastic_interpolated=xNew<IssmDouble>(M);
+				U_viscoelastic_interpolated=xNew<IssmDouble>(M);
+				if(horiz) H_viscoelastic_interpolated=xNew<IssmDouble>(M);
 #endif
+
+
+				xMemCpy<IssmDouble>(G_viscoelastic_interpolated,G_viscoelastic,M);
+				xMemCpy<IssmDouble>(U_viscoelastic_interpolated,U_viscoelastic,M);
+				if(horiz) xMemCpy<IssmDouble>(H_viscoelastic_interpolated,H_viscoelastic,M);
 
 				if(rotation){ //if this cpu handles degree 2
@@ -550,6 +569,6 @@
 #endif
 
-					Pmtf_col_interpolated=love_pmtf_colinear;
-					Pmtf_ortho_interpolated=love_pmtf_ortho;
+					Pmtf_col_interpolated[0]=love_pmtf_colinear[0];
+					Pmtf_ortho_interpolated[0]=love_pmtf_ortho[0];
 					Pmtf_z_interpolated[0]=1.0+love_k[2];
 					Love_tk2_interpolated[0]=love_tk[2];
@@ -587,8 +606,11 @@
 				xDelete<IssmDouble>(G_viscoelastic);
 				xDelete<IssmDouble>(G_viscoelastic_local);
+				xDelete<IssmDouble>(G_viscoelastic_interpolated);
 				xDelete<IssmDouble>(U_viscoelastic);
+				xDelete<IssmDouble>(U_viscoelastic_interpolated);
 				xDelete<IssmDouble>(U_viscoelastic_local);
 				if(horiz){
 					xDelete<IssmDouble>(H_viscoelastic);
+					xDelete<IssmDouble>(H_viscoelastic_interpolated);
 					xDelete<IssmDouble>(H_viscoelastic_local);
 				}
@@ -596,4 +618,13 @@
 					xDelete<IssmDouble>(love_pmtf_colinear);
 					xDelete<IssmDouble>(love_pmtf_ortho);
+					
+					xDelete<IssmDouble>(Love_tk2_interpolated);
+					xDelete<IssmDouble>(Love_th2_interpolated);
+					xDelete<IssmDouble>(Pmtf_col_interpolated);
+					xDelete<IssmDouble>(Pmtf_ortho_interpolated);
+					xDelete<IssmDouble>(Pmtf_z_interpolated);
+					if(horiz){
+						xDelete<IssmDouble>(Love_tl2_interpolated);
+					}
 
 				}
@@ -622,4 +653,5 @@
 	iomodel->DeleteData(&requestedoutputs,numoutputs,"md.solidearth.requested_outputs");
 	/*}}}*/
+
 }/*}}}*/
 
Index: /issm/branches/trunk-larour-SLPS2022/src/c/classes/Elements/Tria.cpp
===================================================================
--- /issm/branches/trunk-larour-SLPS2022/src/c/classes/Elements/Tria.cpp	(revision 27103)
+++ /issm/branches/trunk-larour-SLPS2022/src/c/classes/Elements/Tria.cpp	(revision 27104)
@@ -6657,5 +6657,5 @@
 		LoveRotRSL  = xNewZeroInit<IssmDouble>(nt);
 		LoveRotU    = xNewZeroInit<IssmDouble>(nt);
-		LoveRothoriz= xNewZeroInit<IssmDouble>(nt);
+		if(horiz)LoveRothoriz= xNewZeroInit<IssmDouble>(nt);
 		Grot        = xNewZeroInit<IssmDouble>(3*3*nt); //3 polar motion components * 3 vertices * number of time steps
 		GUrot       = xNewZeroInit<IssmDouble>(3*3*nt);
@@ -6743,4 +6743,8 @@
 			}
 		}
+		/*Free ressources:*/
+		xDelete<IssmDouble>(LoveRotRSL);
+		xDelete<IssmDouble>(LoveRotU);
+		if(horiz)xDelete<IssmDouble>(LoveRothoriz);
 	}
 	/*}}}*/
@@ -7466,4 +7470,7 @@
 	/*Keep track of barystatic contributions:*/
 	barycontrib->Set(this->Sid(),bslcice,bslchydro,bslcbp);
+
+	/*Free ressources:*/
+	xDelete<IssmDouble>(areae);
 
 }
@@ -7782,4 +7789,7 @@
 			/*Save viscous stack now that we updated the values:*/
 			this->inputs->SetArrayInput(viscousenum,this->lid,viscousfield,3*viscousnumsteps);
+
+			/*Free ressources:*/
+			xDelete<IssmDouble>(grdfieldinterp);
 		}
 
@@ -7800,4 +7810,6 @@
 		for(i=0;i<NUMVERTICES;i++) grdfieldout[i]=grdfield[i*nt+0];
 	}
+	/*Free ressources:*/
+	xDelete<IssmDouble>(grdfield);
 
 } /*}}}*/
Index: /issm/branches/trunk-larour-SLPS2022/src/c/classes/GrdLoads.cpp
===================================================================
--- /issm/branches/trunk-larour-SLPS2022/src/c/classes/GrdLoads.cpp	(revision 27103)
+++ /issm/branches/trunk-larour-SLPS2022/src/c/classes/GrdLoads.cpp	(revision 27104)
@@ -19,4 +19,5 @@
 GrdLoads::GrdLoads(int nel,SealevelGeometry* slgeom){ /*{{{*/
 
+	/*allocate:*/
 	vloads=new Vector<IssmDouble>(nel);
 	for (int i=0;i<SLGEOM_NUMLOADS;i++) vsubloads[i]=new Vector<IssmDouble>(slgeom->nbar[i]);
@@ -26,4 +27,11 @@
 
 	vsubsealevelloads=new Vector<IssmDouble>(slgeom->nbar[SLGEOM_OCEAN]);
+
+	/*make sure all pointers that are not allocated are NULL:*/
+	loads=NULL;
+	for (int i=0;i<SLGEOM_NUMLOADS;i++)subloads[i]=NULL;
+	sealevelloads=NULL;
+	subsealevelloads=NULL;
+
 
 }; /*}}}*/
@@ -50,4 +58,11 @@
 	}
 
+	/*Avoid leaks:*/
+	if(loads)xDelete<IssmDouble>(loads);
+	for (int i=0;i<SLGEOM_NUMLOADS;i++){
+		if(subloads[i])xDelete<IssmDouble>(subloads[i]);
+	}
+
+	/*Serialize:*/
 	loads=vloads->ToMPISerial();
 	for (int i=0;i<SLGEOM_NUMLOADS;i++){
@@ -64,9 +79,14 @@
 void GrdLoads::BroadcastSealevelLoads(void){ /*{{{*/
 
+	/*Avoid leakds:*/
+	if(sealevelloads)xDelete<IssmDouble>(sealevelloads);
+	if(subsealevelloads)xDelete<IssmDouble>(subsealevelloads);
+
+	/*Serialize:*/
 	sealevelloads=vsealevelloads->ToMPISerial();
 	subsealevelloads=vsubsealevelloads->ToMPISerial();
 
 } /*}}}*/
-void GrdLoads::SHDegree2Coefficients(IssmDouble* deg2coeff, FemModel* femmodel, SealevelGeometry* slgeom){
+void GrdLoads::SHDegree2Coefficients(IssmDouble* deg2coeff, FemModel* femmodel, SealevelGeometry* slgeom){ /*{{{*/
 
 	IssmDouble area,re, S;
@@ -119,3 +139,3 @@
 	ISSM_MPI_Bcast(&deg2coeff[2],1,ISSM_MPI_DOUBLE,0,IssmComm::GetComm());
 
-} 
+} /*}}}*/ 
Index: /issm/branches/trunk-larour-SLPS2022/src/c/cores/sealevelchange_core.cpp
===================================================================
--- /issm/branches/trunk-larour-SLPS2022/src/c/cores/sealevelchange_core.cpp	(revision 27103)
+++ /issm/branches/trunk-larour-SLPS2022/src/c/cores/sealevelchange_core.cpp	(revision 27104)
@@ -81,4 +81,8 @@
 	/*End profiler*/
 	femmodel->profiler->Stop(SLRCORE);
+
+	/*Free ressources:*/
+	delete slgeom;
+
 }
 /*}}}*/
@@ -265,5 +269,5 @@
 	/*only run if grd was requested, if we are the earth, and we have reached
 	 * the necessary number of time steps dictated by :*/
-	if(!grd)return; 
+	if(!grd)            return;
 	if(count!=frequency)return;
 	femmodel->parameters->FindParam(&iscoupling,IsSlcCouplingEnum);
@@ -357,9 +361,14 @@
 
 		//convergence?
-		if(slcconvergence(loads->vsealevelloads,oldsealevelloads,eps_rel,eps_abs))break;
+		if(slcconvergence(loads->vsealevelloads,oldsealevelloads,eps_rel,eps_abs)){
+			delete oldsealevelloads; break;
+		}
 
 		//early return?
-		if(iterations>=max_nonlinear_iterations)break;
+		if(iterations>=max_nonlinear_iterations){
+			delete oldsealevelloads; break;
+		}
 		iterations++;
+		delete oldsealevelloads;
 	}
 
@@ -381,5 +390,5 @@
 	if(planethasocean){
 		if (!sealevelloading){ //we haven't done so before, so we need to compute the ocean average and area
-			loads->sealevelloads=NULL; //needed to trigger the calculation of areas
+			if(loads->sealevelloads)xDelete<IssmDouble>(loads->sealevelloads); loads->sealevelloads=NULL; //needed to trigger the calculation of areas
 			/*retrieve sea level average  and ocean area:*/
 			for(Object* & object : femmodel->elements->objects){
@@ -423,4 +432,10 @@
 	}
 
+	/*Free ressources:*/
+	delete loads;
+	delete subelementoceanareas;
+	delete oceanareas;
+	xDelete<IssmDouble>(sealevelpercpu); 
+
 }
 /*}}}*/
@@ -607,6 +622,12 @@
 	#endif
 
+	xDelete<IssmDouble>(xxe);
+	xDelete<IssmDouble>(yye);
+	xDelete<IssmDouble>(zze);
+	xDelete<IssmDouble>(areae);
+
 	/*Indicate we won't do this twice: */
 	femmodel->parameters->AddObject(new BoolParam(SealevelchangeGeometryDoneEnum,true));
+
 
 	return;
@@ -675,4 +696,10 @@
 	femmodel->parameters->AddObject(new DoubleVecParam(ZzeEnum,zze,nel));
 	femmodel->parameters->AddObject(new DoubleVecParam(AreaeEnum,areae,nel));
+
+	/*Free ressources:*/
+	xDelete<IssmDouble>(xxe);
+	xDelete<IssmDouble>(yye);
+	xDelete<IssmDouble>(zze);
+	xDelete<IssmDouble>(areae);
 
 	return slgeom;
@@ -873,5 +900,10 @@
 			xDelete<IssmDouble>(m3interp);
 		}
-	}
+		xDelete<IssmDouble>(viscoustimes);
+		xDelete<IssmDouble>(viscouspolarmotion);
+	}
+	xDelete<IssmDouble>(pmtf_col);
+	xDelete<IssmDouble>(pmtf_ortho);
+	xDelete<IssmDouble>(pmtf_z);
 
 } /*}}}*/
Index: /issm/branches/trunk-larour-SLPS2022/src/c/modules/ModelProcessorx/CreateElementsVerticesAndMaterials.cpp
===================================================================
--- /issm/branches/trunk-larour-SLPS2022/src/c/modules/ModelProcessorx/CreateElementsVerticesAndMaterials.cpp	(revision 27103)
+++ /issm/branches/trunk-larour-SLPS2022/src/c/modules/ModelProcessorx/CreateElementsVerticesAndMaterials.cpp	(revision 27104)
@@ -223,4 +223,5 @@
 											{
 												Tria* tria= xDynamicCast<Tria*>(element);
+												if(tria->hmaterial)delete tria->hmaterial; 
 												tria->hmaterial=NULL;
 											}
