Advertisement

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

Started by May 11, 2002 12:28 PM
2 comments, last by kuphryn 22 years, 8 months ago
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
My question is, why are you making things harder than they have to be?

These functions are made to be easily used, sure you can wrap them up into a class or something but don''t go confusing yourself by taking pointers etc. into the equation. You are trying to do network code here, try and keep things simple instead.

Try and sit down with a pen and paper and think things through how you want things to work first.

Personally I find Visio to be good for this.

Once you have done that and have a clear image on what you are trying to do you won''t have these kind of problems.

It seems to me that you want to wrap things up for the sake of wrapping things up if you see my point.

Also I noticed your other posting and by that I strongly suggest that you read the paper from beej (beej''s guide to network programming) or something like that.

I seem to notice that you have some reading to do first (no offence ok)
Advertisement
This is how I think it works. Your SOCKET variable is just an integer handle to a socket. If you are doing something like this:

SOCKET *x = new SOCKET;

you are just creating an integer variable. The Winsock API functions will not free that memory for you. So you have to do:

delete x;

even if you call:

closesocket(*x);

because closesocket just frees up the memory for the actual socket, not the memory for your integer handle to the socket.

Firebird Entertainment
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Thanks.

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

I will adopt that technique.

Kuphryn

This topic is closed to new replies.

Advertisement