Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

how to fix this memory leak. c++

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
sizak
Asked:
sizak
  • 2
1 Solution
 
ExceterCommented:
>> 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
 
sizakAuthor Commented:
wow i feel dumb now.  that fixed it.. I got anotherone for ya tomarrow :)
0
 
ExceterCommented:
You are most welcome.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now