Skip to content

Commit

Permalink
Memory allocated for asteroid perturber computations (BC-405) wasn't …
Browse files Browse the repository at this point in the history
…freed at shutdown, and a file wasn't closed. Of course, those would get automatically closed, but Valgrind couldn't give a clean bill of health unless they were explicitly closed/freed. They are now.
  • Loading branch information
Bill-Gray committed Jul 31, 2023
1 parent a988e48 commit 6140577
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
34 changes: 24 additions & 10 deletions bc405.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ static int bc405_n_asteroids = MAX_BC405_N_ASTEROIDS;
static double bc405_start_jd = 2378495.;
static double bc405_chunk_time = 40.;

static FILE *open_bc405_file( void)
static FILE *open_bc405_file( const bool shutting_down)
{
const char *data_file_name = "bc405.dat";
static FILE *ifile;
static int failure_detected = 0;

if( shutting_down)
{
if( ifile)
fclose( ifile);
ifile = NULL;
return( NULL);
}
if( failure_detected)
return( NULL);
if( !ifile)
Expand Down Expand Up @@ -166,10 +173,9 @@ static void grab_cached_elems( ELEMENTS *elems, const int chunk_number,
{
static int astnums[N_CACHED_ELEMS], chunk_num[N_CACHED_ELEMS];
static ELEMENTS *cache = NULL;
FILE *fp = open_bc405_file( );
FILE *fp;
int i;

assert( fp);
if( !elems)
{
if( cache)
Expand All @@ -179,6 +185,8 @@ static void grab_cached_elems( ELEMENTS *elems, const int chunk_number,
astnums[i] = chunk_num[i] = -1;
return;
}
fp = open_bc405_file( false);
assert( fp);
if( !cache)
cache = (ELEMENTS *)calloc( N_CACHED_ELEMS, sizeof( ELEMENTS));
assert( cache);
Expand Down Expand Up @@ -288,7 +296,7 @@ static int find_and_set_precomputed_data( FILE *precomputed_fp,
const int bc_chunk, int16_t *__restrict posns)
{
const int chunk_size = bc405_n_asteroids * 3;
FILE *bc405_elem_file = open_bc405_file( );
FILE *bc405_elem_file = open_bc405_file( false);
int i;

assert( bc405_elem_file);
Expand Down Expand Up @@ -347,6 +355,7 @@ static double *load_asteroid_masses( void)
if( rval)
rval[i++] = mass;
}
fclose( ifile);
/* rval[0] = 4.747105847157599504245142421641e-10; (solar masses) */
} /* Ceres: special 63 km^3/s^2 value: not now in use */
/* since found to be 62.68 km^3/s^2 by Dawn = 4.7229935634e-10 */
Expand All @@ -360,7 +369,7 @@ int asteroid_position_raw( const int astnum, const double jd,
ELEMENTS elem;
int chunk;

open_bc405_file( );
open_bc405_file( false);
chunk = (int)( (jd - bc405_start_jd) / bc405_chunk_time + .5);
if( chunk < 0)
chunk = 0;
Expand Down Expand Up @@ -394,30 +403,35 @@ int detect_perturbers( const double jd, const double * __restrict xyz,
static FILE *precomputed_fp;
static bool bc405_available = true;
static int n_asteroids_to_use = 0;
double thresh = atof( get_environment_ptr( "ASTEROID_THRESH"));
double thresh;
int i, load_posn0 = 0, load_posn1 = 0, chunk, n_fixed = 0;
const char *fixed_perturber_list = get_environment_ptr( "ASTEROID_PERT_LIST");
const char *fixed_perturber_list;
int fixed_perturbers[MAX_BC405_N_ASTEROIDS];

if( !bc405_available)
return( NO_BC405_FILE);
if( !xyz)
if( !xyz) /* freeing memory, closing cached file pointers */
{
if( precomputed_fp)
fclose( precomputed_fp);
if( masses)
free( masses);
precomputed_fp = NULL;
masses = NULL;
open_bc405_file( true);
grab_cached_elems( NULL, 0, 0);
return( 0);
}
thresh = atof( get_environment_ptr( "ASTEROID_THRESH"));
fixed_perturber_list = get_environment_ptr( "ASTEROID_PERT_LIST");
if( !masses)
masses = load_asteroid_masses( );
if( !masses)
{
bc405_available = false;
return( NO_BC405_FILE);
}
if( !open_bc405_file( ))
if( !open_bc405_file( false))
{
bc405_available = false;
return( NO_BC405_FILE);
Expand Down Expand Up @@ -583,7 +597,7 @@ int main( const int argc, char **argv)
const int asteroid_number = atoi( argv[1]);
const double jd = atof( argv[2]);
const int chunk_number = (int)( (jd - bc405_start_jd) / bc405_chunk_time + .5);
FILE *fp = open_bc405_file( );
FILE *fp = open_bc405_file( false);
ELEMENTS elems;
double posn[4];
int16_t precomputed[bc405_n_asteroids * 3];
Expand Down
3 changes: 3 additions & 0 deletions orb_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4720,6 +4720,8 @@ char *find_numbered_mp_info( const int number); /* mpc_obs.cpp */
int check_for_other_processes( const int locking); /* elem_out.cpp */
int get_temp_dir( char *name, const size_t max_len); /* miscell.cpp */
#endif
int detect_perturbers( const double jd, const double * __restrict xyz,
double *accel);

int clean_up_find_orb_memory( void)
{
Expand All @@ -4746,6 +4748,7 @@ int clean_up_find_orb_memory( void)
pop_all_orbits( );
galactic_confusion( -99., 0.);
find_numbered_mp_info( 0);
detect_perturbers( 0, NULL, NULL);
#if !defined( _WIN32) && !defined( __WATCOMC__)
if( check_for_other_processes( 0))
{
Expand Down

0 comments on commit 6140577

Please sign in to comment.