Solved

how to fix this memory leak. c++

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

816 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

12 Experts available now in Live!

Get 1:1 Help Now