Solved

how to fix this memory leak. c++

Posted on 2003-11-01
3
359 Views
Last Modified: 2010-04-17
ALright here is a function.

OS. freebsd 4.9 .cpp file using dmalloc for memory debugging.

this is the function that has the leak apparently.  its a simple search/replace function.  line 447 is this line

***
result =(char *) malloc(resultLength+1);

****
how can i free this after its returned to the previous function?  or shouldi just have result copied into the memory location for str?

function use is as fallows string = replace(string,"REPLACEME","PUTMEIN-its-place");



char *replace(char *str,char *code,char *val)
{
      char *s,*c;
      char *result,*r;
      char **matchList,**tmp;
      int  matchCount,matchListSize;
      int  strLength,codeLength,valLength,resultLength,len;
      int  i;

      // Make a list of pointers to all the matching positions in str
      matchList =(char **)malloc(GRAIN*sizeof(char *));
      matchListSize = GRAIN;
      matchCount = 0;
      for ( s=str ; *s!='\0' ; s++ ) {
            matchList[matchCount] = s;
            for ( c=code ; *s==*c && *c!='\0' ; *c++ )
            *s++;
            if ( *c=='\0' ) {
                  matchCount++;
                  if ( matchCount==matchListSize ) {
                        matchListSize += GRAIN;
                        if ( (tmp=(char **)realloc(matchList,GRAIN*matchListSize*sizeof(char *)))==NULL ) {
                              free(matchList);
                              matchList=NULL;
                              //return NULL;
                        }
                        matchList = tmp;
                  }
            }
      }

      // Work out how big the result needs to be
      strLength = strlen(str);
      codeLength = strlen(code);
      valLength = strlen(val);
      resultLength = strLength+(matchCount*(valLength-codeLength));
      result =(char *) malloc(resultLength+1);

      // Now copy the data from str to result
      s = str;
      r = result;
      for ( i=0 ; i<matchCount ; i++ ) {
            len = matchList[i]-s;
            memcpy(r,s,len);
            r += len;
            s += len;
            memcpy(r,val,valLength);
            r += valLength;
            s += codeLength;
      }
      memcpy(r,s,strLength-(s-str));
      *(result+resultLength) = '\0';

      free(matchList);
      matchList = NULL;
      return result;
}
0
Comment
Question by:sizak
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Exceter earned 125 total points
ID: 9665067
>> result =(char *) malloc(resultLength+1);
>>
>> ****
>> how can i free this after its returned to the previous function?

free( result );

>> or shouldi just have result copied into the memory location for str?

As far as I'm conerned you should have. It's a replace function. That implies actually replacing the data in the input string. If you don't want the original string harmed you can always say,

char string[] = "This is a test.";
char *temp = strdup( temp );
temp = replace(temp,"REPLACEME","PUTMEIN-its-place");

...

free( temp );

Cheers!
Exceter
0
 

Author Comment

by:sizak
ID: 9665092
wow i feel dumb now.  that fixed it.. I got anotherone for ya tomarrow :)
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9665115
You are most welcome.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question