Memory leak in my String class +operator

There is a memory leak that is occurring in the + operator. For example

str cdir("C:\\FIPM\\DataBases\\TSD\\"),cadd("TsdDaily.dbf") ;
str ctmp = cdir+cadd ;

results in a memory leak. However, it is only perceptible when this code fragment is called a large number of times.

I dont know how to fix the leak? .h and .cpp below..


/* STRING CLASS */

#ifndef __STR_
#define __STR_

/*-------------------------------------------------------------------------*/

#include <string.h>
#include <stdlib.h>
#include <wtypes.h>

/*-------------------------------------------------------------------------*/
/* str is 1 based. */
/*-------------------------------------------------------------------------*/

class str
{
public:
str() ;
str(const int) ;
str(const long) ;
str(const char *) ;
str(const str &) ;
~str() ;

str &operator=(const char *) ;
str &operator=(const wchar_t *) ;
str &operator=(const str &) ;
operator char *() const ;
operator wchar_t *() ;
operator double() ;
char operator[](int) const ;
friend str operator+(const str &,const str &) ;


static int charpos(const char *p_cstr,char csym) ;

private:
char *p_str ;
int nlen ;
wchar_t *p_32str ;
} ;


/*-------------------------------------------------------------------------*/

inline str::~str()
{
delete [] p_str ;

if(p_32str != NULL)
delete [] p_32str ;
}

/*-------------------------------------------------------------------------*/

inline int str::getlen(void) const
{
return(nlen-1) ;
}

/*-------------------------------------------------------------------------*/

inline str::operator char *() const
{
return p_str ;
}

/*-------------------------------------------------------------------------*/

inline str::operator double()
{
return atof(p_str) ;
}

/*-------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------*/

#endif


/* STRING IMPLEMENTATION */

#include "str.h"

/*-------------------------------------------------------------------------*/

str::str()
{
nlen = 1 ;
p_str = new char[nlen] ;
p_str[0] = '\0' ;
p_32str = NULL ;
}

/*-------------------------------------------------------------------------*/

str::str(const int nnum)
{
char buf[18] ;

_itoa(nnum,buf,10) ;
nlen = strlen(buf)+1 ;
p_str = new char[nlen] ;
strcpy(p_str,buf) ;
p_32str = NULL ;
}

/*-------------------------------------------------------------------------*/

str::str(const long nnum)
{
char buf[34] ;

_ltoa(nnum,buf,10) ;
nlen = strlen(buf)+1 ;
p_str = new char[nlen] ;
strcpy(p_str,buf) ;
p_32str = NULL ;
}

/*-------------------------------------------------------------------------*/

str::str(const char *p_cstr)
{
nlen = strlen(p_cstr)+1 ;
p_str = new char[nlen] ;
strcpy(p_str,p_cstr) ;
p_32str = NULL ;
}

/*-------------------------------------------------------------------------*/

str::str(const str &inst)
{
nlen = inst.nlen ;
p_str = new char[nlen] ;
strcpy(p_str,inst.p_str) ;
p_32str = NULL ;
}

/*-------------------------------------------------------------------------*/

str operator+(const str &inst1,const str &inst2)
{
int nlen = inst1.nlen+inst2.nlen+1 ;
char *p_tmp = new char[nlen] ;

strcpy(p_tmp,inst1.p_str) ;
strcat(p_tmp,inst2.p_str) ;
return str(p_tmp) ;
}

/*-------------------------------------------------------------------------*/
Last edited on
closed account (EzwRko23)
1
2
3
4
5
6
7
8
9
10
11
12
13

str operator+(const str &inst1,const str &inst2)
{
int nlen = inst1.nlen+inst2.nlen+1 ;
char *p_tmp = new char[nlen] ;  // <<------ MEMORY LEAK HERE. Never freed.

strcpy(p_tmp,inst1.p_str) ;
strcat(p_tmp,inst2.p_str) ;
str result = str(p_tmp) ;   // <<------ This copies the data pointed by p_tmp; p_tmp is not stored there

delete[] p_tmp;   // <----- Added by xorebxebx to fix the leak.
return result;
}



1
2
3
4
5
6
7
str::str(const char *p_cstr)
{
nlen = strlen(p_cstr)+1 ;
p_str = new char[nlen] ;     
strcpy(p_str,p_cstr) ;
p_32str = NULL ;
}



Last edited on
xorebxebx thank you!
Topic archived. No new replies allowed.