Click here to Skip to main content
14,981,841 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Treeview Question Pin
Michael Dunn7-Sep-07 17:24
sitebuilderMichael Dunn7-Sep-07 17:24 
GeneralRe: Treeview Question Pin
simotix7-Sep-07 19:04
Membersimotix7-Sep-07 19:04 
GeneralRe: Treeview Question Pin
Michael Dunn7-Sep-07 20:06
sitebuilderMichael Dunn7-Sep-07 20:06 
GeneralRe: Treeview Question Pin
simotix8-Sep-07 6:49
Membersimotix8-Sep-07 6:49 
QuestionC question, can't free string from a struct Pin
gizmokaka7-Sep-07 15:06
Membergizmokaka7-Sep-07 15:06 
AnswerRe: C question, can't free string from a struct Pin
Leslie Sanford7-Sep-07 15:28
MemberLeslie Sanford7-Sep-07 15:28 
GeneralRe: C question, can't free string from a struct Pin
gizmokaka7-Sep-07 21:51
Membergizmokaka7-Sep-07 21:51 
AnswerRe: C question, can't free string from a struct Pin
John R. Shaw7-Sep-07 17:30
MemberJohn R. Shaw7-Sep-07 17:30 
Wow! I am glad that was not a physical blow. (Just messing with you Wink | ;) )

First the ‘data’ structure needs values that tells us how many items are in each array contained in the structure, otherwise we will not know how many to free.

The “data” structure contains an array of names (colName), when freeing the names you need to ensure that the pointer to the array and that each pointer to a name is valid or you will get an error (if you are lucky) or unexpected results.

To free the array do the following:
1) Check that ‘colName’ is not NULL; write an initialization function to ensure that it is NULL when it is created, similar to a C++ constructor method. You could just write “data mydata = {0}” when declaring a ‘data’ type and all members will be set to zero (NULL).
2) Before calling ‘free’ for an individual item, make sure that it is not pointing to NULL, before freeing it.
3) After calling ‘free’, set the item (colName[i]) to NULL, so you will not try to free it a second time (by accident).
4) After freeing the individual items, you need to free ‘colName’ and set it to NULL, because it not longer points to any valid data.

Example:
if( data.colName ) /* colName != NULL */
{
    int i;
    for( i=0; i < data.NameCount; ++i )
    {
        if( data.colName[i] ) /* data.colName[i] != NULL */
        {
            free(data.colName[i]);
            data.colName[i] = NULL;
        }
    }
    free(data.colName);
    data.colName = NULL;
}


If you understand all of that, then welcome to the world of real programmers!

P.S. I am ignoring the fact that your example is freeing something called colNum, which does not exist.



INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

GeneralRe: C question, can't free string from a struct Pin
gizmokaka7-Sep-07 22:01
Membergizmokaka7-Sep-07 22:01 
GeneralRe: C question, can't free string from a struct Pin
John R. Shaw7-Sep-07 22:34
MemberJohn R. Shaw7-Sep-07 22:34 
GeneralRe: C question, can't free string from a struct Pin
gizmokaka7-Sep-07 23:05
Membergizmokaka7-Sep-07 23:05 
GeneralRe: C question, can't free string from a struct Pin
John R. Shaw7-Sep-07 23:42
MemberJohn R. Shaw7-Sep-07 23:42 
GeneralRe: C question, can't free string from a struct Pin
Leslie Sanford7-Sep-07 22:34
MemberLeslie Sanford7-Sep-07 22:34 
GeneralRe: C question, can't free string from a struct Pin
John R. Shaw7-Sep-07 22:54
MemberJohn R. Shaw7-Sep-07 22:54 
GeneralRe: small sample of where problem ocurres. Pin
gizmokaka7-Sep-07 22:34
Membergizmokaka7-Sep-07 22:34 
GeneralRe: small sample of where problem ocurres. Pin
John R. Shaw7-Sep-07 22:42
MemberJohn R. Shaw7-Sep-07 22:42 
GeneralRe: small sample of where problem ocurres. Pin
markkuk7-Sep-07 23:12
Membermarkkuk7-Sep-07 23:12 
GeneralRe: small sample of where problem ocurres. Pin
gizmokaka7-Sep-07 23:21
Membergizmokaka7-Sep-07 23:21 
GeneralRe: small sample of where problem ocurres. Pin
John R. Shaw7-Sep-07 23:24
MemberJohn R. Shaw7-Sep-07 23:24 
GeneralRe: small sample of where problem ocurres. Pin
Leslie Sanford7-Sep-07 23:42
MemberLeslie Sanford7-Sep-07 23:42 
GeneralRe: Another qestion on the same subject. Pin
gizmokaka8-Sep-07 0:20
Membergizmokaka8-Sep-07 0:20 
GeneralRe: small sample of where problem ocurres. Pin
Russell'7-Sep-07 23:33
MemberRussell'7-Sep-07 23:33 
QuestionClosing a view Pin
bob169727-Sep-07 11:08
Memberbob169727-Sep-07 11:08 
AnswerRe: Closing a view Pin
Russell'7-Sep-07 23:02
MemberRussell'7-Sep-07 23:02 
GeneralRe: Closing a view Pin
bob169728-Sep-07 4:44
Memberbob169728-Sep-07 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.