Solved

how to fix this memory leak. c++

Posted on 2003-11-01
3
356 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now