Alternate method for sum_species

fixed memory leak when copying phreeqc instance with llnl.dat

git-svn-id: svn://136.177.114.72/svn_GW/phreeqc3/trunk@7473 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
David L Parkhurst 2013-02-19 19:40:11 +00:00
parent 3461797c95
commit 1342a5c580
4 changed files with 74 additions and 0 deletions

View File

@ -2559,6 +2559,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc)
llnl_count_temp = pSrc->llnl_count_temp;
if (llnl_count_temp > 0)
{
llnl_temp = (LDBLE *) free_check_null(llnl_temp);
llnl_temp = (LDBLE *) PHRQ_malloc((size_t) llnl_count_temp * sizeof(LDBLE));
if (llnl_temp == NULL) malloc_error();
memcpy(llnl_temp, pSrc->llnl_temp, (size_t) llnl_count_temp * sizeof(LDBLE));
@ -2566,6 +2567,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc)
llnl_count_adh = pSrc->llnl_count_adh;
if (llnl_count_adh > 0)
{
llnl_adh = (LDBLE *) free_check_null(llnl_adh);
llnl_adh = (LDBLE *) PHRQ_malloc((size_t) llnl_count_adh * sizeof(LDBLE));
if (llnl_adh == NULL) malloc_error();
memcpy(llnl_adh, pSrc->llnl_adh, (size_t) llnl_count_adh * sizeof(LDBLE));
@ -2573,6 +2575,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc)
llnl_count_bdh = pSrc->llnl_count_bdh;
if (llnl_count_bdh > 0)
{
llnl_bdh = (LDBLE *) free_check_null(llnl_bdh);
llnl_bdh = (LDBLE *) PHRQ_malloc((size_t) llnl_count_bdh * sizeof(LDBLE));
if (llnl_bdh == NULL) malloc_error();
memcpy(llnl_bdh, pSrc->llnl_bdh, (size_t) llnl_count_bdh * sizeof(LDBLE));
@ -2580,6 +2583,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc)
llnl_count_bdot = pSrc->llnl_count_bdot;
if (llnl_count_bdot > 0)
{
llnl_bdot = (LDBLE *) free_check_null(llnl_bdot);
llnl_bdot = (LDBLE *) PHRQ_malloc((size_t) llnl_count_bdot * sizeof(LDBLE));
if (llnl_bdot == NULL) malloc_error();
memcpy(llnl_bdot, pSrc->llnl_bdot, (size_t) llnl_count_bdot * sizeof(LDBLE));
@ -2587,6 +2591,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc)
llnl_count_co2_coefs = pSrc->llnl_count_co2_coefs;
if (llnl_count_co2_coefs > 0)
{
llnl_co2_coefs = (LDBLE *) free_check_null(llnl_co2_coefs);
llnl_co2_coefs = (LDBLE *) PHRQ_malloc((size_t) llnl_count_co2_coefs * sizeof(LDBLE));
if (llnl_co2_coefs == NULL) malloc_error();
memcpy(llnl_co2_coefs, pSrc->llnl_co2_coefs, (size_t) llnl_count_co2_coefs * sizeof(LDBLE));

View File

@ -1890,6 +1890,7 @@ protected:
/* new after release of Version 3 */
std::map<std::string, std::vector < std::string> > sum_species_map;
std::map<std::string, std::vector < std::string> > sum_species_map_db;
friend class PBasic;
friend class ChartObject;

View File

@ -1503,6 +1503,7 @@ sum_match_species(const char *mytemplate, const char *name)
return (tot);
}
#else
#ifndef SUM_SPECIES_METHOD_2
/* ---------------------------------------------------------------------- */
LDBLE Phreeqc::
sum_match_species(const char *mytemplate, const char *name)
@ -1552,6 +1553,72 @@ sum_match_species(const char *mytemplate, const char *name)
}
return (tot);
}
#else
/* ---------------------------------------------------------------------- */
LDBLE Phreeqc::
sum_match_species(const char *mytemplate, const char *name)
/* ---------------------------------------------------------------------- */
{
int i;
LDBLE tot;
struct elt_list *next_elt;
count_elts = 0;
paren_count = 0;
tot = 0;
if (sum_species_map.find(mytemplate) == sum_species_map.end())
{
if (sum_species_map_db.find(mytemplate) == sum_species_map_db.end())
{
std::vector<std::string> species_list_db;
for (i = 0; i < count_s; i++)
{
struct species *s_ptr = s[i];
if (match_elts_in_species(s_ptr->name, mytemplate) == TRUE)
{
species_list_db.push_back(s_ptr->name);
}
}
sum_species_map_db[mytemplate] = species_list_db;
}
std::vector<std::string> &species_list = (sum_species_map_db.find(mytemplate))->second;
std::vector<std::string> species_list_x;
for (size_t i=0; i < species_list.size(); i++)
{
struct species *s_ptr = s_search(species_list[i].c_str());
if (s_ptr->in == TRUE)
{
species_list_x.push_back(species_list[i]);
}
}
sum_species_map[mytemplate] = species_list_x;
}
std::vector<std::string> &species_list = (sum_species_map.find(mytemplate))->second;
for (size_t i=0; i < species_list.size(); i++)
{
struct species *s_ptr = s_search(species_list[i].c_str());
if (s_ptr->in == FALSE) continue;
if (name == NULL)
{
tot += s_ptr->moles;
}
else
{
for (next_elt = s_ptr->next_elt; next_elt->elt != NULL;
next_elt++)
{
if (strcmp(next_elt->elt->name, name) == 0)
{
tot += next_elt->coef * s_ptr->moles;
break;
}
}
}
}
return (tot);
}
#endif
#endif
/* ---------------------------------------------------------------------- */

View File

@ -1131,6 +1131,7 @@ build_model(void)
max_s_x = MAX_S;
// clear sum_species_map, which is built from s_x
sum_species_map_db.clear();
sum_species_map.clear();
space((void **) ((void *) &s_x), INIT, &max_s_x,