Free memory from species and reaction arrays (to satisfy some valgrind).
authorDariusz Murakowski <murakdar@mit.edu>
Tue, 21 Apr 2015 18:18:09 +0000 (14:18 -0400)
committerDariusz Murakowski <murakdar@mit.edu>
Tue, 21 Apr 2015 18:18:09 +0000 (14:18 -0400)
ss.cpp

diff --git a/ss.cpp b/ss.cpp
index 33d0a29..f1487a6 100644 (file)
--- a/ss.cpp
+++ b/ss.cpp
@@ -72,38 +72,38 @@ void run(RunParameters_SS &r, unsigned seed) {
     //double mu = 1.0e-1;
 
     // initialize virus
-    VirusSpecies s1("I");
-    species.push_back(&s1);
+    VirusSpecies *s1 = new VirusSpecies("I");
+    species.push_back(s1);
     if (r.importState) {
         importState(r);      // <infile>.st
         for (size_t i=0; i<r.initPop.size(); i++) {
             Virus V(H, r.mu, r.initPop[i]);
             unsigned int N = (unsigned int) r.n * r.initFrac[i];
-            s1.pop[V] = N;
-            s1.count += N;
+            s1->pop[V] = N;
+            s1->count += N;
         }
     }
     else {
-        s1.count = r.n;
-        s1.pop[Virus(H,r.mu)] = s1.count;   // default mu = 6.0e-5
+        s1->count = r.n;
+        s1->pop[Virus(H,r.mu)] = s1->count;   // default mu = 6.0e-5
     }
 
     Species_parray print_spec;
-    print_spec.push_back(&s1);
+    print_spec.push_back(s1);
 
-    if (r.useEpitope)  importEpitope(r,species,reactions,&s1,print_spec);    // <infile>.ep
+    if (r.useEpitope)  importEpitope(r,species,reactions,s1,print_spec);    // <infile>.ep
 
     // V -> V + V'
     VirusReaction* r1 = new VirusReaction(r.rate_s);  // s_k
     //r1->rate_constant = 1.5;
     r1->H = H;
-    r1->V = &s1;
+    r1->V = s1;
     reactions.push_back(r1);
     // V -> 0
     VirusDeathReaction* r2 = new VirusDeathReaction(r.rate_u);  // u
     //r2->rate_constant = 0.5;
     r2->H = H;
-    r2->V = &s1;
+    r2->V = s1;
     reactions.push_back(r2);
 
 
@@ -113,6 +113,23 @@ void run(RunParameters_SS &r, unsigned seed) {
     simulate(reactions, species, r.t_end, r.max_steps, r.sample_interval, print_spec);
 
     gsl_rng_free(rnd);  //Free up memory from random number generator
+
+    // clean up reaction array
+    for (Rxn_parray::iterator  it = reactions.begin(),
+                              end = reactions.end();
+      it != end; ++it) {
+        delete (*it);
+    }
+    reactions.clear();
+
+    // clean up species array
+    for (Species_parray::iterator  it = species.begin(),
+                                  end = species.end();
+      it != end; ++it) {
+        delete (*it);
+    }
+    species.clear();
+
 }