memory leak - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

memory leak

bazzanobazzano Posts: 15Member

Does anyone know why i am getting memory leaks could someone please help me thanks...
[code]
/****************************************************************************
* COSC1098/1283 - Assignment #2 - Tennis Store
* Programming Principles 2A/Programming Techniques
* Author :
* Student Number :
* Yallara Username :
* Start up code provided by Steven Burrows - [email protected]
****************************************************************************/

#include "ts.h"
#include "ts_utility.h"
#include "ts_options.h"

/****************************************************************************
* Function readRestOfLine() is used for buffer clearing. Source:
* https://inside.cs.rmit.edu.au/~pmcd/teaching/C-Prog/CourseDocuments/
* FrequentlyAskedQuestions/
****************************************************************************/
void readRestOfLine()
{
int c;

/* Read until the end of the line or end-of-file. */
while ((c = fgetc(stdin)) != '
' && c != EOF)
;

/* Clear the error and end-of-file flags. */
clearerr(stdin);
}

/****************************************************************************
* Initialises the system to a safe empty state.
****************************************************************************/
void systemInit(TennisStoreType* ts)
{

ts -> headCust = NULL;
ts -> headStock = NULL;
ts -> customerCount;
ts -> stockCount;

}

/****************************************************************************
* Loads all data into the system.
****************************************************************************/
void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
{

/* declaration of variables*/
char* stockID;
char* description;
char* custID;
char* surname;
char* firstName;
char* address;
char* suburb;
char sTmp[100];
char cTmp[100];
char* unitpricePtr;
char* stocklevelPtr;
char* postCodePtr;
char* phoneNumPtr;

float unitprice;
int stocklevel;
int postCode;
int phoneNum;



/* pointing nodes to customer variables*/

CustomerNodeType* newCust;
CustomerNodeType* curCust;
CustomerNodeType* prevCust;

StockNodeType* newStock;
StockNodeType* curStock;
StockNodeType* prevStock;
/* variables for files*/

FILE *cFile;
FILE *sFile;

if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
stockFile,"r")) == NULL)
{
fprintf(stderr, "error opening file
");
exit(EXIT_FAILURE);

}
else
{

printf("file was opened correctly");
}

while(fgets(cTmp, 100, cFile ) != NULL)
{

/* tokenizing variables*/

custID = strtok(cTmp, ",");
surname = strtok(NULL, ",");
firstName = strtok(NULL, ",");
address = strtok(NULL, ",");
suburb = strtok(NULL, ",");
postCodePtr = strtok(NULL, ",");
phoneNumPtr = strtok(NULL,"/0");
postCode = atoi(postCodePtr);
phoneNum = atoi(phoneNumPtr);

/* allocating memory using malloc for nodes*/

prevCust = malloc(sizeof(*prevCust));
curCust = malloc(sizeof(*curCust));
newCust = malloc(sizeof(*newCust));

/* start of linked list*/

curCust = ts -> headCust;
prevCust = NULL;

while(curCust !=NULL)
{
prevCust = curCust;

curCust = curCust -> nextCust;

}
/* copy the string variables into newCust node*/

strcpy(newCust->custID,custID);
strcpy(newCust->surname,surname);
strcpy(newCust->firstName,firstName);
strcpy(newCust->address,address);
strcpy(newCust->suburb,suburb);

/* newCust is postCode*/

newCust->postCode=postCode;

/* newCust is phoneNum*/

newCust->phoneNum=phoneNum;

/* printing the preloaded data*/
printf("
%s",newCust -> custID);
printf(" %s", newCust -> firstName);
printf(" %s", newCust -> surname);
printf(" %s", newCust -> address);
printf(" %s", newCust -> suburb);
printf(" %d", newCust -> postCode);
printf(" %d", newCust -> phoneNum);

/* keep count of each customer in loop*/

ts -> customerCount++;

/* start of linked list*/

if(prevCust == NULL)
{
ts ->headCust = newCust;

}
else
{

prevCust -> nextCust = newCust;

}
/* if there is a previous node get the next node and make
it the new node*/
}
fclose(cFile);



while(fgets(sTmp, 900, sFile) != NULL)
{
/* tokenizing each field from comma*/

stockID = strtok(sTmp, ",");
description = strtok(NULL, ",");
unitpricePtr = strtok(NULL,",");
stocklevelPtr = strtok(NULL, "/0");

/* cast the Ptr character to an integers*/

/* unitprice = atof(unitpricePtr);
stocklevel = atoi(stocklevelPtr);
*/
prevStock = malloc(sizeof(*prevStock));
curStock = malloc(sizeof(*curStock));
newStock = malloc(sizeof(*newStock));

curStock = ts -> headStock;
prevStock = NULL;

while(curStock != NULL)
{
prevStock = curStock;

curStock = curStock -> nextStock;

}

/* printing the file*/
printf("
%s", newStock->stockID);
printf(" %s", newStock->description);
printf(" %d", newStock->unitPrice);
printf(" %d", newStock->stockLevel);

ts -> stockCount++;

if(prevStock == NULL)
{
ts->headStock=newStock;
}
else
{
prevStock->nextStock=newStock;
}


}
fclose(sFile);




}

/****************************************************************************
* Deallocates memory used in the program.
****************************************************************************/
void systemFree(TennisStoreType* ts)
{
CustomerNodeType* curCust;
CustomerNodeType* tmpCust = ts -> headCust;

curCust = tmpCust;


while(curCust !=NULL)
{
tmpCust = tmpCust -> nextCust;
free(curCust);
curCust = tmpCust;

}
printf("
successfully deallocated memory");

}



[/code]

Comments

  • LundinLundin Posts: 3,711Member
    :
    : Does anyone know why i am getting memory leaks could someone please help me thanks...
    : [code]
    : /****************************************************************************
    : * COSC1098/1283 - Assignment #2 - Tennis Store
    : * Programming Principles 2A/Programming Techniques
    : * Author :
    : * Student Number :
    : * Yallara Username :
    : * Start up code provided by Steven Burrows - [email protected]
    : ****************************************************************************/
    :
    : #include "ts.h"
    : #include "ts_utility.h"
    : #include "ts_options.h"
    :
    : /****************************************************************************
    : * Function readRestOfLine() is used for buffer clearing. Source:
    : * https://inside.cs.rmit.edu.au/~pmcd/teaching/C-Prog/CourseDocuments/
    : * FrequentlyAskedQuestions/
    : ****************************************************************************/
    : void readRestOfLine()
    : {
    : int c;
    :
    : /* Read until the end of the line or end-of-file. */
    : while ((c = fgetc(stdin)) != '
    ' && c != EOF)
    : ;
    :
    : /* Clear the error and end-of-file flags. */
    : clearerr(stdin);
    : }
    :
    : /****************************************************************************
    : * Initialises the system to a safe empty state.
    : ****************************************************************************/
    : void systemInit(TennisStoreType* ts)
    : {
    :
    : ts -> headCust = NULL;
    : ts -> headStock = NULL;
    : ts -> customerCount;
    : ts -> stockCount;
    :
    : }
    :
    : /****************************************************************************
    : * Loads all data into the system.
    : ****************************************************************************/
    : void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
    : {
    :
    : /* declaration of variables*/
    : char* stockID;
    : char* description;
    : char* custID;
    : char* surname;
    : char* firstName;
    : char* address;
    : char* suburb;
    : char sTmp[100];
    : char cTmp[100];
    : char* unitpricePtr;
    : char* stocklevelPtr;
    : char* postCodePtr;
    : char* phoneNumPtr;
    :
    : float unitprice;
    : int stocklevel;
    : int postCode;
    : int phoneNum;
    :
    :
    :
    : /* pointing nodes to customer variables*/
    :
    : CustomerNodeType* newCust;
    : CustomerNodeType* curCust;
    : CustomerNodeType* prevCust;
    :
    : StockNodeType* newStock;
    : StockNodeType* curStock;
    : StockNodeType* prevStock;
    : /* variables for files*/
    :
    : FILE *cFile;
    : FILE *sFile;
    :
    : if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
    : stockFile,"r")) == NULL)
    : {
    : fprintf(stderr, "error opening file
    ");
    : exit(EXIT_FAILURE);
    :
    : }
    : else
    : {
    :
    : printf("file was opened correctly");
    : }
    :
    : while(fgets(cTmp, 100, cFile ) != NULL)
    : {
    :
    : /* tokenizing variables*/
    :
    : custID = strtok(cTmp, ",");
    : surname = strtok(NULL, ",");
    : firstName = strtok(NULL, ",");
    : address = strtok(NULL, ",");
    : suburb = strtok(NULL, ",");
    : postCodePtr = strtok(NULL, ",");
    : phoneNumPtr = strtok(NULL,"/0");
    : postCode = atoi(postCodePtr);
    : phoneNum = atoi(phoneNumPtr);
    :
    : /* allocating memory using malloc for nodes*/
    :
    : prevCust = malloc(sizeof(*prevCust));
    : curCust = malloc(sizeof(*curCust));
    : newCust = malloc(sizeof(*newCust));
    [blue]
    Shouldn't this be:
    prevCust = malloc(sizeof(CustomerNodeType));
    ...
    [/blue]
    :
    : /* start of linked list*/
    :
    : curCust = ts -> headCust;
    : prevCust = NULL;
    :
    : while(curCust !=NULL)
    : {
    : prevCust = curCust;
    :
    : curCust = curCust -> nextCust;
    :
    : }
    : /* copy the string variables into newCust node*/
    :
    : strcpy(newCust->custID,custID);
    : strcpy(newCust->surname,surname);
    : strcpy(newCust->firstName,firstName);
    : strcpy(newCust->address,address);
    : strcpy(newCust->suburb,suburb);
    :
    : /* newCust is postCode*/
    :
    : newCust->postCode=postCode;
    :
    : /* newCust is phoneNum*/
    :
    : newCust->phoneNum=phoneNum;
    :
    : /* printing the preloaded data*/
    : printf("
    %s",newCust -> custID);
    : printf(" %s", newCust -> firstName);
    : printf(" %s", newCust -> surname);
    : printf(" %s", newCust -> address);
    : printf(" %s", newCust -> suburb);
    : printf(" %d", newCust -> postCode);
    : printf(" %d", newCust -> phoneNum);
    :
    : /* keep count of each customer in loop*/
    :
    : ts -> customerCount++;
    :
    : /* start of linked list*/
    :
    : if(prevCust == NULL)
    : {
    : ts ->headCust = newCust;
    :
    : }
    : else
    : {
    :
    : prevCust -> nextCust = newCust;
    :
    : }
    : /* if there is a previous node get the next node and make
    : it the new node*/
    : }
    : fclose(cFile);
    :
    :
    :
    : while(fgets(sTmp, 900, sFile) != NULL)
    : {
    : /* tokenizing each field from comma*/
    :
    : stockID = strtok(sTmp, ",");
    : description = strtok(NULL, ",");
    : unitpricePtr = strtok(NULL,",");
    : stocklevelPtr = strtok(NULL, "/0");
    :
    : /* cast the Ptr character to an integers*/
    :
    : /* unitprice = atof(unitpricePtr);
    : stocklevel = atoi(stocklevelPtr);
    : */
    : prevStock = malloc(sizeof(*prevStock));
    : curStock = malloc(sizeof(*curStock));
    : newStock = malloc(sizeof(*newStock));
    :
    : curStock = ts -> headStock;
    : prevStock = NULL;
    :
    : while(curStock != NULL)
    : {
    : prevStock = curStock;
    :
    : curStock = curStock -> nextStock;
    :
    : }
    :
    : /* printing the file*/
    : printf("
    %s", newStock->stockID);
    : printf(" %s", newStock->description);
    : printf(" %d", newStock->unitPrice);
    : printf(" %d", newStock->stockLevel);
    :
    : ts -> stockCount++;
    :
    : if(prevStock == NULL)
    : {
    : ts->headStock=newStock;
    : }
    : else
    : {
    : prevStock->nextStock=newStock;
    : }
    :
    :
    : }
    : fclose(sFile);
    :
    :
    :
    :
    : }
    :
    : /****************************************************************************
    : * Deallocates memory used in the program.
    : ****************************************************************************/
    : void systemFree(TennisStoreType* ts)
    : {
    : CustomerNodeType* curCust;
    : CustomerNodeType* tmpCust = ts -> headCust;
    :
    : curCust = tmpCust;
    :
    :
    : while(curCust !=NULL)
    : {
    : tmpCust = tmpCust -> nextCust;
    : free(curCust);
    : curCust = tmpCust;
    :
    : }
    : printf("
    successfully deallocated memory");
    :
    : }
    :
    :
    :
    : [/code]
    :

  • bazzanobazzano Posts: 15Member
    That still dosnt help i still get memory leaks
  • LundinLundin Posts: 3,711Member
    : That still dosnt help i still get memory leaks
    :

    You changed all 6 places where you have malloc?
  • stoberstober Posts: 9,765Member ✭✭✭
    : [blue]
    : Shouldn't this be:
    : prevCust = malloc(sizeof(CustomerNodeType));
    : ...
    : [/blue]
    : :

    It is not uncommon to do it the way the OP posted. Either method works, but I prefer to do it the way you posted because I can quickly determine what is being allocated. It isn't so obvious using the op's method.
  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-9-29 6:8:23[/red][/b][hr]
    :
    : Does anyone know why i am getting memory leaks could someone please help me thanks...
    : [code]
    : /****************************************************************************
    : * COSC1098/1283 - Assignment #2 - Tennis Store
    : * Programming Principles 2A/Programming Techniques
    : * Author :
    : * Student Number :
    : * Yallara Username :
    : * Start up code provided by Steven Burrows - [email protected]
    : ****************************************************************************/
    :
    : #include "ts.h"
    : #include "ts_utility.h"
    : #include "ts_options.h"
    :
    : /****************************************************************************
    : * Function readRestOfLine() is used for buffer clearing. Source:
    : * https://inside.cs.rmit.edu.au/~pmcd/teaching/C-Prog/CourseDocuments/
    : * FrequentlyAskedQuestions/
    : ****************************************************************************/
    : void readRestOfLine()
    : {
    : int c;
    :
    : /* Read until the end of the line or end-of-file. */
    : while ((c = fgetc(stdin)) != '
    ' && c != EOF)
    : ;
    :
    : /* Clear the error and end-of-file flags. */
    : clearerr(stdin);
    : }
    :
    : /****************************************************************************
    : * Initialises the system to a safe empty state.
    : ****************************************************************************/
    : void systemInit(TennisStoreType* ts)
    : {
    :
    : ts -> headCust = NULL;
    : ts -> headStock = NULL;
    : ts -> customerCount;
    : ts -> stockCount;
    :
    : }
    :
    : /****************************************************************************
    : * Loads all data into the system.
    : ****************************************************************************/
    : void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
    : {
    :
    : /* declaration of variables*/
    : char* stockID;
    : char* description;
    : char* custID;
    : char* surname;
    : char* firstName;
    : char* address;
    : char* suburb;
    : char sTmp[100];
    : char cTmp[100];
    : char* unitpricePtr;
    : char* stocklevelPtr;
    : char* postCodePtr;
    : char* phoneNumPtr;
    :
    : float unitprice;
    : int stocklevel;
    : int postCode;
    : int phoneNum;
    :
    :
    :
    : /* pointing nodes to customer variables*/
    :
    : CustomerNodeType* newCust;
    : CustomerNodeType* curCust;
    : CustomerNodeType* prevCust;
    :
    : StockNodeType* newStock;
    : StockNodeType* curStock;
    : StockNodeType* prevStock;
    : /* variables for files*/
    :
    : FILE *cFile;
    : FILE *sFile;
    :
    : if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
    : stockFile,"r")) == NULL)
    : {
    : fprintf(stderr, "error opening file
    ");
    : exit(EXIT_FAILURE);
    :
    : }
    : else
    : {
    :
    : printf("file was opened correctly");
    : }
    :
    : while(fgets(cTmp, 100, cFile ) != NULL)
    : {
    :
    : /* tokenizing variables*/
    :
    : custID = strtok(cTmp, ",");
    : surname = strtok(NULL, ",");
    : firstName = strtok(NULL, ",");
    : address = strtok(NULL, ",");
    : suburb = strtok(NULL, ",");
    : postCodePtr = strtok(NULL, ",");
    : phoneNumPtr = strtok(NULL,"/0");
    : postCode = atoi(postCodePtr);
    : phoneNum = atoi(phoneNumPtr);
    :
    : /* allocating memory using malloc for nodes*/
    :
    [red] DELETE the next two lines [/red]
    : prevCust = malloc(sizeof(*prevCust));
    : curCust = malloc(sizeof(*curCust));
    [red] ^^^ delete the above TWO lines[/red]
    : newCust = malloc(sizeof(*newCust));
    :
    : /* start of linked list*/
    :
    : curCust = ts -> headCust;
    [red] ^^^ memory leak here. curCust was previously allocated, and
    now you are just tossing the pointer to the allocated memory into
    the bit bucket.[/red]
    : prevCust = NULL;
    :
    : while(curCust !=NULL)
    : {
    : prevCust = curCust;
    :
    : curCust = curCust -> nextCust;
    :
    : }
    : /* copy the string variables into newCust node*/
    :
    : strcpy(newCust->custID,custID);
    : strcpy(newCust->surname,surname);
    : strcpy(newCust->firstName,firstName);
    : strcpy(newCust->address,address);
    : strcpy(newCust->suburb,suburb);
    :
    : /* newCust is postCode*/
    :
    : newCust->postCode=postCode;
    :
    : /* newCust is phoneNum*/
    :
    : newCust->phoneNum=phoneNum;
    :
    : /* printing the preloaded data*/
    : printf("
    %s",newCust -> custID);
    : printf(" %s", newCust -> firstName);
    : printf(" %s", newCust -> surname);
    : printf(" %s", newCust -> address);
    : printf(" %s", newCust -> suburb);
    : printf(" %d", newCust -> postCode);
    : printf(" %d", newCust -> phoneNum);
    :
    : /* keep count of each customer in loop*/
    :
    : ts -> customerCount++;
    :
    : /* start of linked list*/
    :
    : if(prevCust == NULL)
    : {
    : ts ->headCust = newCust;
    :
    : }
    : else
    : {
    :
    : prevCust -> nextCust = newCust;
    :
    : }
    : /* if there is a previous node get the next node and make
    : it the new node*/
    : }
    : fclose(cFile);
    :
    :
    :
    : while(fgets(sTmp, 900, sFile) != NULL)
    : {
    : /* tokenizing each field from comma*/
    :
    : stockID = strtok(sTmp, ",");
    : description = strtok(NULL, ",");
    : unitpricePtr = strtok(NULL,",");
    : stocklevelPtr = strtok(NULL, "/0");
    :
    : /* cast the Ptr character to an integers*/
    :
    : /* unitprice = atof(unitpricePtr);
    : stocklevel = atoi(stocklevelPtr);
    : */
    : prevStock = malloc(sizeof(*prevStock));
    : curStock = malloc(sizeof(*curStock));
    : newStock = malloc(sizeof(*newStock));
    :
    : curStock = ts -> headStock;
    : prevStock = NULL;
    :
    : while(curStock != NULL)
    : {
    : prevStock = curStock;
    :
    : curStock = curStock -> nextStock;
    :
    : }
    :
    : /* printing the file*/
    : printf("
    %s", newStock->stockID);
    : printf(" %s", newStock->description);
    : printf(" %d", newStock->unitPrice);
    : printf(" %d", newStock->stockLevel);
    :
    : ts -> stockCount++;
    :
    : if(prevStock == NULL)
    : {
    : ts->headStock=newStock;
    : }
    : else
    : {
    : prevStock->nextStock=newStock;
    : }
    :
    :
    : }
    : fclose(sFile);
    :
    :
    :
    :
    : }
    :
    : /****************************************************************************
    : * Deallocates memory used in the program.
    : ****************************************************************************/
    : void systemFree(TennisStoreType* ts)
    : {
    : CustomerNodeType* curCust;
    : CustomerNodeType* tmpCust = ts -> headCust;
    :
    : curCust = tmpCust;
    :
    :
    : while(curCust !=NULL)
    : {
    : tmpCust = tmpCust -> nextCust;
    : free(curCust);
    : curCust = tmpCust;
    :
    : }
    : printf("
    successfully deallocated memory");
    :
    : }
    :
    :
    :
    : [/code]
    :



  • LundinLundin Posts: 3,711Member
    : : [blue]
    : : Shouldn't this be:
    : : prevCust = malloc(sizeof(CustomerNodeType));
    : : ...
    : : [/blue]
    : : :
    :
    : It is not uncommon to do it the way the OP posted. Either method works, but I prefer to do it the way you posted because I can quickly determine what is being allocated. It isn't so obvious using the op's method.
    :

    Ok, I was a bit uncertain about it sence the contents of the pointer had not been initialized. But I realize now that the sizeof() is handled at compile-time, so it should work. Looks a bit dangerous though :-)

Sign In or Register to comment.