0

Program is leaking memory and not able to fix it... This Program is reading data from text files and after reading data, it perform certain operation on data during this it leaks memory. Device has very limited memory & flash drive due to this I cannot run memory leaks checking tools. Please advise to fix the memory leak issue Please find code snippet below

int LanguageRequiredData(void)
{

   char *data=NULL;
   int retValue = 0 ;

   retValue = GetString_English(&data);
   if(retValue>0 && strlen(data)>0)
   {
      // Do Some Operation
   }

   if (data!=NULL)
  {
      Mem_free(data);
      data = NULL;
  }
}


int GetString_English(char **data)
{

    int retValue = 0 ;

    retValue = File_LoadContent(LANGSENGFILE,&(*data));

    return retValue;
}


int File_LoadContent (char *file, char **content) 
{

   long size = File_Size(file); 
   char buf[256]={};

   memset(buf,0x00,sizeof(buf));
   if (*content)
  {
    Mem_free(*content);
  }
  *content = (char*) Mem_alloc((size+1) * sizeof(char));

  TFILE * fd; fd=File_Open(file,"r"); if (fd==NULL) return 0;
  while (File_Gets(buf,sizeof(buf),fd)!=NULL)
  {
    strcat(*content,buf);
    memset(buf,0x00,sizeof(buf));
  }
  File_Close(fd); return 1;
}


void * Mem_alloc(size_t size)
{
   int    i;
   void * ptr = NULL;

   for (i = 0; i < 2; i++)
   {
      ptr = malloc(size);

      if (ptr)
      {
         break;
      }
   }

  if (ptr)
  {
      memset(ptr, 0, size);
   }

  return ptr;
}

void Mem_free(void * ptr)
{    
   if (ptr != NULL)
  { 
     free(ptr);
  }

  ptr = NULL;    
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
JosephCenk
  • 327
  • 1
  • 5
  • 15
  • 1
    and what did you try till time? – Sourav Ghosh May 16 '17 at 06:18
  • Welcome to Stack Overflow! Please show your research/debugging effort so far. Please read [Ask] page first. – Sourav Ghosh May 16 '17 at 06:18
  • 4
    You mind creating a [___MCVE___](http://stackoverflow.com/help/mcve)? – Sourav Ghosh May 16 '17 at 06:19
  • Unrelated, but your `Mem_free` function is not necessary. You can just call `free` because it's OK to call `free` on a NULL pointer. – Jabberwocky May 16 '17 at 06:27
  • And what's this `for` loop in your `Mem_alloc` function? That's really fishy. – Jabberwocky May 16 '17 at 06:30
  • this code is leaking memory or not ? how can we confirm this – JosephCenk May 16 '17 at 06:31
  • @JosephCenk in your question you pretend that your program is leaking memory and now you ask __how__ you can confirm that the program is leaking memory or not. So what is your question in first place? – Jabberwocky May 16 '17 at 06:32
  • @MichaelWalz for loop will break if it allocates memory... I am sure it is leaking memory after this code execution it corrupts memory. – JosephCenk May 16 '17 at 06:34
  • Are you sure of your `File_size` function, from `strcat` man page : If dest is not large enough, program behavior is unpredictable – Ôrel May 16 '17 at 06:52
  • @Ôrel File size is simple function long File_Size (char *file) { TFILE *fd; long size=0; fd=File_Open(file,"r"); if (fd==NULL) return 0; fseek(fd, 0L, SEEK_END); size = File_Tell(fd); File_Close(fd); return size; } – JosephCenk May 16 '17 at 06:56
  • 1
    @JosephCenk I don't see anything obviously wrong in the code you posted. The problem may be elsewhere. – Jabberwocky May 16 '17 at 07:04
  • As said in the deleted answer, avoid casting the result of malloc and posting pseudo-code. – Badda May 16 '17 at 08:02
  • 1
    By the way, your `Mem_alloc()` function, which initialises an area to zero, can be done using the standard library function `calloc()`. – cdarke May 16 '17 at 08:26
  • 1
    `char buf[256]={}; memset(buf,0x00,sizeof(buf));` can be replace with `char buf[256]={0};` – cdarke May 16 '17 at 08:28
  • My advice is to avoid malloc. If you have enough RAM to malloc buffers for the largest file read, then you can do a static allocation big enough to accommodate that largest file. Then you don't need to use malloc at all. Problem solved. – DisappointedByUnaccountableMod May 16 '17 at 20:40
  • @barny I just have 64MB RAM available... if I load all data into stack then it will crash... so dynamic memory allocation is best option available.. – JosephCenk May 17 '17 at 05:50
  • I said a STATIC allocation, declared outside a function, so not on a stack. Personally I'd jump through hoops to avoid using malloc, it's a recipe for this sort of question. – DisappointedByUnaccountableMod May 17 '17 at 11:16

1 Answers1

0

This part seems suspect:

  while (File_Gets(buf,sizeof(buf),fd)!=NULL)
  {
    strcat(*content,buf);
    memset(buf,0x00,sizeof(buf));
  }

Specifically the strcat(). Is File_Gets() null-terminating what it writes to buf[]? If not then the strcat() may be reading/copying beyond the bounds of buf[], since it requires null-termination to know when to stop.

EDIT: I should point out that this is one of the reasons to recommend strncat() over strcat(). Using the "n" variants of the string functions (i.e. strncat(), strncpy(), strncmp()) helps prevent buffer overruns and is generally a good practice.

Andrew Cottrell
  • 3,312
  • 3
  • 26
  • 41