Skip to content

Commit 7a80de8

Browse files
authored
Merge pull request #434 from cse-sim/fix-memory-leaks
Fix memory leaks
2 parents 3e80514 + 8736464 commit 7a80de8

File tree

10 files changed

+71
-47
lines changed

10 files changed

+71
-47
lines changed

src/cgcomp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,13 @@ struct AIRNET_SOLVER
22832283
: an_pParent( pParent), an_jac(), an_V1(), an_V2(), an_mdotAbs(nullptr),
22842284
an_didLast(nullptr), an_nz( 0)
22852285
{ }
2286-
~AIRNET_SOLVER() { }
2286+
~AIRNET_SOLVER()
2287+
{
2288+
delete an_mdotAbs;
2289+
an_mdotAbs = nullptr;
2290+
delete an_didLast;
2291+
an_didLast = nullptr;
2292+
}
22872293

22882294
RC an_Calc(int iV);
22892295

src/cgsolar.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int slrCalcJDays[13] = // julian day of year for which to do solar table calcula
7676
//16 14 15 14 14 10 16 15 15 15 14 10 (days of months)
7777
};
7878

79-
#if (SLRCALCS & 1)
79+
#if (0 && SLRCALCS & 1)
8080
static SLLOCDAT* Locsolar = NULL; // Ptr to solar info structure for current calculation.
8181
// Released by slfree from tp_LocDone().
8282
// Address saved by slinit for other slpak calls.
@@ -97,7 +97,7 @@ RC TOPRAT::tp_LocInit() // location-initializer for CSE
9797
// slpak remembers its location internally.
9898

9999
#if (SLRCALCS & 1)
100-
Locsolar = slinit(RAD(latitude), // latitude (to radians)
100+
slinit(RAD(latitude), // latitude (to radians)
101101
RAD(longitude), // longitude
102102
timeZone, // time zone
103103
elevation); // site altitude (ft)
@@ -111,7 +111,7 @@ RC TOPRAT::tp_LocInit() // location-initializer for CSE
111111
RC TOPRAT::tp_LocDone() // Free location related dm stuff: Locsolar
112112
{
113113
#if (SLRCALCS & 1)
114-
slfree(&Locsolar); // free solar data struct, null ptr, slpak.cpp. Redundant call ok.
114+
slfree(); // free solar data struct, null ptr, slpak.cpp. Redundant call ok.
115115
#endif
116116
return RCOK;
117117
} // TOPRAT::tp_LocDone
@@ -384,7 +384,7 @@ void FC makHrSgt( // make solar tables for an hour for current month
384384
* INT(Top.tp_date.month-1), INT(slrCalcJDays[Top.tp_date.month]) );
385385
#endif
386386

387-
// Initialize slpak for a standard day near middle of month. slselect(Locsolar) in effect. Note restored at exit.
387+
// Initialize slpak for a standard day near middle of month. Note restored at exit.
388388

389389
slday( slrCalcJDays[ Top.tp_date.month], // julian date for this month's solar calcs
390390
sltmLST ); // say use local standard time

src/cse.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ _DLLImpExp int cse( // CSE main function, called by console main(), Windows Wi
356356
pMsgCallBack = _pMsgCallBack;
357357
#endif // CSE_DLL
358358

359+
dmInitMemoryChecking();
360+
359361
isCallBackAbort = 0; // clear abort flag (1 if prior run aborted)
360362

361363
//-- nested entry protection: DLL only has 1 copy of variables even if 2 copies of app loaded
@@ -1224,7 +1226,7 @@ LOCAL INT cse3( INT argc, const char* argv[])
12241226
vrPrintf( vrTimes, "\n\n%sTiming info --\n\n", pfx);
12251227
int tmrLimit = (TestOptions & 2) ? TMR_COUNT : TMR_TOTAL + 1;
12261228
for (i = 0; i < tmrLimit; i++)
1227-
vrTmrDisp( vrTimes, i, pfx); // timer.cpp
1229+
tmrVrDisp( vrTimes, i, pfx); // timer.cpp
12281230
}
12291231

12301232
// Output timing info & any remaining log/err msgs to report file -- in particular, input error that prevented unspool above.

src/cul.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,6 +4207,7 @@ LOCAL void FC drefAddI( // add deferred reference table entry -- general inner
42074207
drfp->i = i; // entry subscr,
42084208
drfp->fn = fn; // field number.
42094209
drfp->toB = toB; // what is being referenced: rat,
4210+
dmfree( DMPP( drfp->toName)); // free any prior toName before overwrite
42104211
drfp->toName = strsave( toName); // entry name.
42114212
drfp->defO = defO; // default owner from context
42124213
// .. get file/line info for error messages using cutok.cpp fcn

src/dmpak.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ RC dmIncRef( DMP* pp, int erOp/*=ABT*/) // duplicate block or ++ref count (as im
199199
dmRefCount( *pp)++;
200200
return RCOK;
201201
} // dmIncRef
202+
//-----------------------------------------------------------------------------
203+
void dmInitMemoryChecking()
204+
{
205+
206+
#if CSE_COMPILER == CSE_COMPILER_MSVC
207+
#if defined( _DEBUG)
208+
// Get the current bits
209+
int tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
210+
211+
tmp |= _CRTDBG_LEAK_CHECK_DF; // check for memory leaks at program exit
212+
213+
// Set the new bits
214+
_CrtSetDbgFlag(tmp);
215+
216+
int request = -1; // set with debugger to trap specific blocks
217+
if (request != -1)
218+
_CrtSetBreakAlloc(request);
219+
#endif
220+
#endif
221+
} // dmInitMemoryChecking
202222
///////////////////////////////////////////////////////////////////////////////
203223

204224
// end of dmpak.cpp

src/dmpak.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ RC dmfree( DMP *pp); // checks stack, no FC
3939
RC dmIncRef( DMP *pp, int erOp=ABT);
4040
RC dmPrivateCopy( DMP *pp, int erOp=ABT);
4141
int dmIsGoodPtr( DMP p, const char* s, int erOp);
42+
void dmInitMemoryChecking();
4243

4344
#ifdef DEBUG2
4445
void FC dmeatmem( USI sz);

src/solar.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const float SolarConstant = 434.f; // previous value, source unknown
143143

144144
// pointer to current solar/location info thing. Set by slinit() / slselect().
145145
// Used by slday(), sldec, slsurfhr, slha, sldircos, slaniso
146-
LOCAL SLLOCDAT * slloccur;
146+
static SLLOCDAT* slloccur = nullptr;
147147

148148
/*----------------------- LOCAL FUNCTION DECLARATIONS ---------------------*/
149149

@@ -385,15 +385,14 @@ float slVProfAng( // vertical profile angle
385385
return profAng;
386386
} // slVProfAng
387387
//======================================================================
388-
SLLOCDAT *FC
389-
slinit(/* Allocate, init, and select a SLLOCAT structure */
388+
void slinit(/* Allocate, init, and select a SLLOCAT structure */
390389

391-
float rlat, // latitude, +=N, radians
392-
float rlong, // longitude, +=W (non-standard), radians
393-
float tzn, // time zone (hrs west of GMT. EST=5, PST=8, etc. )
394-
float siteElev) // site elevation (ft above sea level).
395-
// Used only with global/direct functions (eg dirsimx.c, gshift.c)
396-
// SO passing 0 is generally harmless.
390+
float rlat, // latitude, +=N, radians
391+
float rlong, // longitude, +=W (non-standard), radians
392+
float tzn, // time zone (hrs west of GMT. EST=5, PST=8, etc. )
393+
float siteElev) // site elevation (ft above sea level).
394+
// Used only with global/direct functions (eg dirsimx.c, gshift.c)
395+
// SO passing 0 is generally harmless.
397396

398397
/* Returns pointer to SLLOCDAT structure initialized for specified location.
399398
This location is now also the current location for subsequent
@@ -411,30 +410,20 @@ slinit(/* Allocate, init, and select a SLLOCAT structure */
411410
pSlr->tzn = tzn; /* time zone, hours west of GMT */
412411
pSlr->siteElev = siteElev; /* site elevation, ft */
413412
pSlr->pressureRatio = (float)exp(-0.0001184 * 0.3048 * siteElev);
414-
/* site pressure ratio: nominal ratio of surface pressure to sea level pressure.
415-
Formula appears in Perez code (dirsim.c) and is documented in SERI Consensus
416-
Summary (full reference above), p. 17.
417-
NOTE: needs reconciliation with psychro1.c:psyAltitude. 8-9-90 */
418-
return slselect(
419-
pSlr); // make current: ptr for other slpak fcns. ret ptr, for later slselects and slfree
413+
// site pressure ratio: nominal ratio of surface pressure to sea level pressure.
414+
// Formula appears in Perez code (dirsim.c) and is documented in SERI Consensus
415+
// Summary (full reference above), p. 17.
416+
// NOTE: needs reconciliation with psychro1.c:psyAltitude. 8-9-90
417+
418+
slfree();
419+
slloccur = pSlr; // make current: ptr for other slpak fcns
420420
} // slinit
421421
//======================================================================
422-
SLLOCDAT *FC slselect(/* Sets current solar/location data to that given by arg */
423-
424-
SLLOCDAT *pSlr) /* pointer returned by an earlier slinit call */
425-
426-
/* returns pSlr as convenience (new feature 8-9-90) */
422+
void FC slfree() // Free a SLLOCDAT structure
427423
{
428-
return (slloccur = pSlr); /* set file-global for slpak fcns to use */
429-
} /* slselect */
424+
delete slloccur; // free heap storage occupied by object
425+
slloccur = nullptr;
430426

431-
//======================================================================
432-
void FC slfree( // Free a SLLOCDAT structure
433-
434-
SLLOCDAT **ppSlr) // ptr to ptr. nop if ptr is NULL; ptr is set NULL: redundant calls ok.
435-
{
436-
delete *ppSlr; // free heap storage occupied by object
437-
*ppSlr = NULL; // erase no-longer-valid pointer
438427
} // slfree
439428

440429
//======================================================================

src/solar.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ x void FC slhourfromalt(float alt);
118118
#endif
119119
void FC slsdc(float azm, float tilt, float dcos[3]);
120120
float slVProfAng(float relAzm, float cosZen);
121-
SLLOCDAT* FC slinit(float rlat, float rlong, float tzn, float siteElev);
122-
SLLOCDAT* FC slselect(SLLOCDAT* pSlr);
123-
void FC slfree(SLLOCDAT** ppSlr);
121+
void slinit(float rlat, float rlong, float tzn, float siteElev);
122+
void FC slfree();
124123
void FC slday(DOY doy, int timetype, int options = 0);
125124
void FC sldec(float sldec, int timetype);
126125
int slsurfhr(float sdircos[3], int ihr, float* pCosi, float* pAzm = NULL,

src/timer.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ struct TMRDATA
1818
int tcalls; // # of calls
1919
LLI tbeg; // time at beg of current timing, ticks
2020
LLI ttot; // total time for timer, ticks
21-
WStr tname; // name of timer
22-
TMRDATA(const char* _tname = "") : tcalls(0), tbeg(0), ttot(0), tname(_tname)
21+
std::string tname; // name of timer
22+
TMRDATA() : tcalls(0), tbeg(0), ttot(0), tname()
2323
{}
24-
};
25-
static WVect< TMRDATA> tmrtab;
26-
//------------------------------------------------------------------------------
24+
void tmrSet(const char* tmrName)
25+
{ tname = tmrName;
26+
tcalls = 0;
27+
tbeg = ttot = 0;
28+
} // TMRDATA::tmrSet
29+
}; // struct TMRDATA
30+
//-----------------------------------------------------------------------------
31+
static std::vector< TMRDATA> tmrtab;
32+
//-----------------------------------------------------------------------------
2733
static double tmrSecsPerTick = -1.;
2834
//-----------------------------------------------------------------------------
2935
static double tmrTicksToSecs(
@@ -115,7 +121,7 @@ void tmrInit( // Initialize (but do *NOT* start) a timer
115121
{
116122
if (size_t( tmr) >= tmrtab.size())
117123
tmrtab.resize(tmr+5);
118-
new (&tmrtab[ tmr]) TMRDATA(tmrName);
124+
tmrtab[tmr].tmrSet(tmrName);
119125
tmrSetupIf(); // constants etc if needed
120126
} // tmrInit
121127
//========================================================================
@@ -164,7 +170,7 @@ void tmrdisp( // Display current data for a timer
164170
} // tmrdisp
165171
#endif
166172
//========================================================================
167-
RC vrTmrDisp( // Display current data for a timer
173+
RC tmrVrDisp( // Display current data for a timer
168174

169175
int vrh, // open virtual report to which to display results
170176
int tmr, // Timer number
@@ -180,6 +186,6 @@ RC vrTmrDisp( // Display current data for a timer
180186
return vrPrintf( vrh, "%s%20s: Time = %-7.2f Calls = %-8ld T/C = %-0.4f\n",
181187
pfx, tmrtab[tmr].tname.c_str(), t, tmrtab[tmr].tcalls, t/nc );
182188

183-
} // vrTmrDisp
189+
} // tmrVrDisp
184190

185191
// end of timer.cpp

src/timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ void tmrStart( int tmr);
1010
void tmrStop( int tmr);
1111
double tmrCurTot( int tmr);
1212
void tmrDisp( FILE *f, int tmr, double delta); // may be if 0'd
13-
RC vrTmrDisp( int vrh, int tmr, const char* pfx="", double delta=0.);
13+
RC tmrVrDisp( int vrh, int tmr, const char* pfx="", double delta=0.);
1414

1515
// End of timer.h
1616

0 commit comments

Comments
 (0)