Solved

how to fix this memory leak. c++

Posted on 2003-11-01
3
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans

627 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