Computing.Net > Forums > Programming > Linked List de-allocation of pointe

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Linked List de-allocation of pointe

Reply to Message Icon

Name: Daemon Rose
Date: April 18, 2005 at 08:11:16 Pacific
OS: WinXp
CPU/Ram: 512
Comment:

Okay well I have a method that adds nodes to my linked list, now this seems to be written the same was that everyone else online writes their linked list, however when my program exits I always get a bunhc of memory leaks from the line in my code where "temp = new SatID;". I have tried many different ways to delete this pointer, however the program always crashes if I do. Can you guys tell me why no one else seems to have to delete this pointer, and yet I keep getting error msgs??? Thanks for the help,
void CEmailDownloaderDlg::OnCheckAndAdd()
{
// THIS SECTION IS FOR GETTING RESULTS FROM THE DATABASE ////////////
MYSQL_RES *result;
MYSQL_ROW row;
unsigned int num_fields;
char * qry = new char[300];
//SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable ORDER BY full_name;
sprintf(qry, "SELECT SatelliteID, SatEmail from satellite");
//int slt = mysql_query(conn33,qry);

if (mysql_query(conn33, qry)) {
//Error querry didn't complete
return;
}
else { // query succeeded, process any data returned by it
result = mysql_store_result(conn33);
if (!result) {// there are no rows
//error
return;
}
num_fields = mysql_num_fields(result);

while ((row = mysql_fetch_row(result))) {
SatID * temp;
SatID * temp2;
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for(int i = 0; i < (int)num_fields; i++)
{
sprintf(qry, "[%.*s] ", (int) lengths[i], row[i]);
CString mystring = qry;
}

temp = new SatID;

temp->SatelliteID=atoi(row[0]);
temp->SatEmail=row[1];
temp->next_SatID=NULL;

if (mySatIDs==NULL) {
mySatIDs = temp;
continue;
}
temp2=mySatIDs;
while (temp2->next_SatID!=NULL)
temp2=temp2->next_SatID;

temp2->next_SatID=temp;
}
}
delete [] qry;

}

(BTW - my structure is defined as)
struct SatID {
int SatelliteID;
CString SatEmail;
SatID * next_SatID;
};

and mySatIDs is originally set to = NULL



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: April 19, 2005 at 13:00:39 Pacific
Reply:

I hope i am following your code correctly, if so, in your deconstructor try something like this:

SatID *temp;
while(MySatIDs != null)
{
    while(MySatIDs->next_SatID != null)

    {
        temp = MySatIDs->next_SatID;
    }
    temp = null;
    delete temp;
}
MySatIDs = null;
delete MySatIDs;


Chi

They mostly come at night...mostly


0

Response Number 2
Name: Daemon Rose
Date: April 20, 2005 at 07:43:36 Pacific
Reply:

Chi, thank you very much for the response, that is EXACTLY correct. I kept wondering how I could delete that pointer without it deleting the things it was pointing to...LoL, now I know :) Thanks for the help.


0

Response Number 3
Name: Chi Happens
Date: April 20, 2005 at 11:46:00 Pacific
Reply:

i think there may be an issue actually:

SatID *temp;
while(MySatIDs != null)
{
temp = MySatIDs;
while(temp->next_SatID != null)

{
temp = temp->next_SatID;
}
temp = null;
delete temp;
}
MySatIDs = null;
delete MySatIDs;


try this.

Chi

They mostly come at night...mostly


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Linked List de-allocation of pointe

Linked List www.computing.net/answers/programming/linked-list/2064.html

linked list in c www.computing.net/answers/programming/linked-list-in-c/7408.html

Linked list again www.computing.net/answers/programming/linked-list-again/7041.html