home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C++ distructor question

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1534
Discussions: 200,985, Posts: 2,379,852, Members: 246,348
Old March 20th, 2002, 06:21 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Feb 2002
Posts: 20
C++ distructor question

When i create a new socket object, the value in the point to the object is not NULL
if (IncomingSocket == NULL) IncomingSocket = new Socket;
This way I dont have any memory leaks, etc
Now when I need to destroy the socket...
if (IncomingSocket != NULL) delete IncomingSocket;
This way i dont try to delete a NULL object and avoid a memory fault.
The problem is that I must set the point = NULL;
IncomgingSocket=NULL;
This is a pain when I have in a multithreaded app when sycronization can realy ruin my day.
It would be nice if in the destructor, I could set that pointer value to null before returning.
// ------------------------------------------------------------------
Socket::~Socket()
{
if (iSock > -1) closesocket(iSock);

if (szPeerName) delete [] szPeerName;
//*** Somehow set the point = NULL;
}
Anyone know how to do this? Only trick I can think of gets realy nasty with stack calls.
tony
TonyM123 is offline   Reply With Quote
Old March 29th, 2002, 05:53 PM     #2 (permalink)
Member
 
SpiffGQ's Avatar
 
Join Date: Oct 2001
Location: United States
Posts: 408
Why would you need to set the pointer to NULL? Isn't that kind of the point of delete?
SpiffGQ is offline   Reply With Quote
Old March 29th, 2002, 06:19 PM     #3 (permalink)
Ultimate Member
 
FalcomPSX's Avatar
 
Join Date: Oct 2001
Location: SoCal.
Posts: 2,853
Send a message via ICQ to FalcomPSX Send a message via AIM to FalcomPSX
not really t048, the delete keyword deallocates the memory allocated with new. just setting the pointer to NULL leaves the memory allocated. though i'm not sure how to accomplish what wants...
__________________
FalcomPSX

Last edited by FalcomPSX : March 30th, 2002 at 03:54 AM.
FalcomPSX is offline   Reply With Quote
Old March 30th, 2002, 12:01 AM     #4 (permalink)
Member
 
SpiffGQ's Avatar
 
Join Date: Oct 2001
Location: United States
Posts: 408
What would this accomplish?
SpiffGQ is offline   Reply With Quote
Old April 1st, 2002, 02:47 AM     #5 (permalink)
Member
 
Join Date: Feb 2002
Posts: 161
If you don't set the pointer to NULL, and you attempt to use it again, your program will most likely do strange things, as the memory it points to is no longer guaranteed to be valid. Also, if you try to delete it again without setting it to NULL, your program will most likely die a horrible death. Pointers should almost always be set to NULL after they have been deleted.

TonyM123: Although it's not an elegant solution, you could always take advantage of the preprocessor. Something like:
#define SafeDelete(p) { delete p; p = NULL; }

I don't think there is a good way of doing what you are looking for.
Martee is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3063)
Charges against non-tippers dropped.. (20)
Health Care Rationing (11)
Delete an OS (16)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
windows vista security holes (19)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
[F@H SPAM 11/16/09] ! 1/2 months to.. (39)
Recent Discussions
[F@H SPAM 11/16/09] ! 1/2 months to r.. (39)
Share an Easy Way to Convert DVD and .. (0)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
Desktop Calendar Application (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
Delete an OS (16)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)


All times are GMT -4. The time now is 03:16 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28