API "Clean Up" Functions & delete Pointers :: Winsock

This is a discussion on API "Clean Up" Functions & delete Pointers :: Winsock within the Windows Programming forums, part of the Platform Specific Boards category; Hi. I am stuck at on design that include extensive use of pointer. I prefer using point because with pointers, ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    API "Clean Up" Functions & delete Pointers :: Winsock

    Hi.

    I am stuck at on design that include extensive use of pointer. I prefer using point because with pointers, I have more options such as when to create the data structure and when to delele the data structure. Furthermore, I have more options when passing the pointers to functions. Nonetheless, pointers can be tricky when dealing with some API and other Windows based tools such as MFC.

    Here is the problem. In Winsock programming, there are API functions that does "clean up," according to Anthony Jones and Jim Ohlund, authors of Network Programming for Microsoft Windows, Second Edition. Some of these API functions include these functions:

    // closesocket(...) // you pass in a sock
    // freeaddrinfo(...) // you pass a pointer to a struct addinfo
    // WSACleanUp() // you pass in nothing

    Okay. Those are just a few "clean up" functions for Winsock. I am sure there are some other ones. Here is my question.

    As I have mentioned that I prefer using pointer and creating a new data structure and deleting the pointer when needed. That technique gives me more flexibility. However, with respect the the "clean up" functions above, how do they react to pointers? For example:

    // sock *mySocket = socket(...); // mySocket is a pointer to a socket

    Now I close it.

    // closesocket(*mysocket);

    Now, should I still call delete mySocket afterward? My main concern is I do not know whether each the API function mentioned above the pointer if you pass it a pointer.

    The same scenarios holds for freeinddrinfo and WSACleanup().

    In the case of a struct inaddrinfo, you pass in a pointer. However, do you delete that pointer afterard?

    In the case of WSAData, do you delete the pointer to a WSAData structure after calling WSACleanup()?

    Currently, I *do* delete all pointers even after calling WSACleanup().

    Thanks,
    Kuphryn

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    The fact that your program doesn't crash while deleting pointers after clean up basically tells you that those functions don't delete the pointer. Usually, calling delete on an already deleted pointer (that the programmer has failed to set to NULL) results in an unstable program or nice page fault error.

    As for closesocket(), inspection of the actual workings of the function reveals why you must release pointers afterwards. SOCKET's are simply defined as ints, so your SOCKET * becomes an int *. closesocket(), among other things, just sets the int at that address to INVALID_SOCKET so that the socket may be reassigned and used later on, and thus does not delete anything.

    freeaddrinfo() cleans the structure referred to by the pointer, but doesn't kill the pointer.

    And yes, delete the pointer to the WSAData struct since it's not automatically freed by anyone and only used by WSAStartup(). Most codes on the Net even declare the struct as a local variable and let it expire at the end of their Winsock initialization function.

    So, delete everything.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    Several members at Anandtech and CodeGuru mentioned a easy technique. They both implied that call delete only if you have called new.

    I will adopt that technique.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reg pointers to functions
    By sowmi in forum C Programming
    Replies: 1
    Last Post: 06-22-2007, 01:08 AM
  2. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM
  3. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 10:59 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM
  5. delete pointers, but not the pointed object
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2001, 08:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21