Skip to content

Commit

Permalink
Various memory related issued fixed.
Browse files Browse the repository at this point in the history
- Fixes a memory leak
- Avoids some warnings when debugging python code with valgrind
- Fixes an issue with IAS15 array sizes (closes #805)
  • Loading branch information
hannorein committed Nov 26, 2024
1 parent 5bfccb2 commit 4cabad0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
10 changes: 8 additions & 2 deletions rebound/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import sys
import warnings

# Avoid unboxing strings. See
# https://sourceforge.net/p/ctypes/mailman/message/8469497/
class allocated_c_char_p(c_char_p):
pass

import types

### The following enum and class definitions need to
Expand Down Expand Up @@ -281,9 +286,10 @@ def __eq__(self, other):
def diff(self, other):
if not isinstance(other,Simulation):
return NotImplemented
clibrebound.reb_simulation_diff_char.restype = c_char_p
clibrebound.reb_simulation_diff_char.restype = allocated_c_char_p
output = clibrebound.reb_simulation_diff_char(byref(other), byref(self))
print(output.decode("utf-8"))
print(output.value.decode("utf-8"))
clibrebound.reb_free(output)

def __add__(self, other):
if not isinstance(other,Simulation):
Expand Down
11 changes: 6 additions & 5 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ void reb_input_fields(struct reb_simulation* r, FILE* inf, enum reb_simulation_b
reb_tree_add_particle_to_tree(r, l);
}
}
if (r->ri_ias15.at){
// Assume that all arrays were saved whenever ri_ias15.at was saved.
// Only 3*N entries got saved.
r->ri_ias15.N_allocated = 3*r->N;
}
// Commented out on Nov 26 2024. Not sure why this was added. Might be for an older SA version.
// if (r->ri_ias15.at){
// // Assume that all arrays were saved whenever ri_ias15.at was saved.
// // Only 3*N entries got saved.
// r->ri_ias15.N_allocated = 3*r->N;
// }
r->ri_whfast512.recalculate_constants = 1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/particle.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "rebound.h"
#include "tree.h"
#include "boundary.h"
Expand Down Expand Up @@ -52,8 +53,10 @@ static void reb_simulation_add_local(struct reb_simulation* const r, struct reb_
return;
}
while (r->N_allocated<=r->N){
unsigned int old_N_allocated = r->N_allocated;
r->N_allocated = r->N_allocated ? r->N_allocated * 2 : 128;
r->particles = realloc(r->particles,sizeof(struct reb_particle)*r->N_allocated);
memset(r->particles + old_N_allocated, 0, (r->N_allocated - old_N_allocated) * sizeof(struct reb_particle));
}

r->particles[r->N] = pt;
Expand Down
6 changes: 6 additions & 0 deletions src/rebound.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ void reb_clear_pre_post_pointers(struct reb_simulation* const r){
}

void reb_simulation_init(struct reb_simulation* r){
memset(r, 0, sizeof(struct reb_simulation));
r->rand_seed = reb_tools_get_rand_seed();
reb_simulation_reset_function_pointers(r);
r->t = 0;
Expand Down Expand Up @@ -950,6 +951,11 @@ int reb_check_fp_contract(){
return r1 != r2;
}

// Wrapper to free pointers from python.
void reb_free(void* p){
free(p);
}

#ifdef _WIN32

void PyInit_librebound() {};
Expand Down
2 changes: 2 additions & 0 deletions src/rebound.h
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,8 @@ DLLEXPORT void reb_particles_transform_jacobi_to_inertial_posvel(struct reb_part
DLLEXPORT void reb_particles_transform_jacobi_to_inertial_pos(struct reb_particle* const particles, const struct reb_particle* const p_j, const struct reb_particle* const p_mass, const unsigned int N, const unsigned int N_active);
DLLEXPORT void reb_particles_transform_jacobi_to_inertial_acc(struct reb_particle* const particles, const struct reb_particle* const p_j, const struct reb_particle* const p_mass, const unsigned int N, const unsigned int N_active);

DLLEXPORT void reb_free(void* p);

// Democratic heliocentric coordinates
DLLEXPORT void reb_particles_transform_inertial_to_democraticheliocentric_posvel(const struct reb_particle* const particles, struct reb_particle* const p_h, const unsigned int N, const unsigned int N_active);
DLLEXPORT void reb_particles_transform_democraticheliocentric_to_inertial_pos(struct reb_particle* const particles, const struct reb_particle* const p_h, const unsigned int N, const unsigned int N_active);
Expand Down

0 comments on commit 4cabad0

Please sign in to comment.