1

I am working with sockets and I don't have access to the TCP library. It seems that when the clients disconnects, it deletes the memory of the object but it doesn't make it NULL. How would I be able to check for that?

Client != NULL   => true

Currently I have the following, which crashes:

Client is 0xddddddddddddddd

slawekwin
  • 6,270
  • 1
  • 44
  • 57
ioan
  • 751
  • 1
  • 9
  • 26
  • 5
    `0xddd...dd` indicates that the memory was not initialized (in Debug mode). Seems like you forgot yourself to set it to `NULL`. – CompuChip Aug 16 '16 at 11:28
  • @CompuChip Agree, in debug version. Release doesn't fill unitilased data. – Jacek Cz Aug 16 '16 at 11:29
  • 2
    please post the code/outputs and not pictures of it – slawekwin Aug 16 '16 at 11:29
  • 1
    Why do you expect a deleted pointer will be `nullptr`? C++ doesn't do such services. – ikh Aug 16 '16 at 11:33
  • 1
    Destroying an object has no effect on any variables that refer to it. – molbdnilo Aug 16 '16 at 11:34
  • We have no idea what library you're talking about. Why don't you ask the author(s) instead? There must surely be some "on disconnect" callback handler you can write the necessary code in. – Lightness Races in Orbit Aug 16 '16 at 11:51
  • Possible duplicate of [How to catch the null pointer exception?](http://stackoverflow.com/questions/1823721/how-to-catch-the-null-pointer-exception) – Jonathan Mee Aug 16 '16 at 12:06
  • You can't 'detect' this unfortunately. The variable `Client` is your own variable that is not under the control of the third party library, so if that library is destroying the memory pointed to by your variable then you're relying on their documentation telling you what to expect, or some other way of detecting it. – LordWilmore Aug 16 '16 at 12:15
  • @JonathanMee: Not at all a duplicate, `nullptr` is a well-defined value whereas `0xddddddddd` is a sign of an uninitialized value. – MSalters Aug 16 '16 at 12:38
  • I am initialising the value in the constructor `ConnectionSocket::ConnectionSocket() :Client(NULL) {}`, but I am going to add the try catch for null pointers and see if the error occurs afterwards. I will compromise to the fact that try catch is cpu expensive. This function is called over and over again, and it works.. but after a while it just deletes the memory. This is the UDK TCP sockets btw, this class in particular is FSocket. – ioan Aug 16 '16 at 12:40
  • 1
    @GamsterKatalin: You can't use `try...catch` to catch a null pointer in C++ (unlike C# and Java). – MSalters Aug 16 '16 at 12:43
  • @MSalters I think that's true as well, as I think I have tried it in the past few days. So the guy with the "possible duplicate" is wrong. – ioan Aug 16 '16 at 12:44
  • @GamsterKatalin You cannot use a `try`-`catch` for this: http://stackoverflow.com/questions/1823721/how-to-catch-the-null-pointer-exception – Jonathan Mee Aug 16 '16 at 12:44
  • I can't believe UDK engine has this: `void FSocketSubsystemBSD::DestroySocket(FSocket* Socket){delete Socket; }` God bless – ioan Aug 16 '16 at 12:48
  • @MSalters I don't understand why you're saying it isn't a duplicate. He's trying to detect if the pointer references valid memory. You can't do that in C++. You need to use another construct for keeping track of whether your memory is valid. – Jonathan Mee Aug 16 '16 at 12:49
  • @GamsterKatalin You don't like that it doesn't set `Socket` to `nullptr`? This just means if you're going to use UDK, you must identify where `DestroySocket` may be called, and test if it happened. – Jonathan Mee Aug 16 '16 at 12:51
  • I guess it is kind of a duplicate. Therefore I am not going to mark @MSalters' answer as accepted I guess. The library is just silly that all it does is it just deletes the memory. I don't even know when it decides to do that. But I am going to find a workaround. Thanks everybody. – ioan Aug 16 '16 at 12:52

2 Answers2

0

As the comments correctly indicate, you can't check against a deleted pointer. For that reason, even a half-decent C++ framework will tell you when it deletes a pointer. A decent framework will use smart pointers for this.

If you've found code that randomly deletes objects, replace it instead of trying to find workarounds. This won't be the only problem that you're going to have with such code.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

This is kind of typical case you will need to deal with in network programming, as other comments said, you can't detect if a pointer is deleted or not.

The solution I personally use is shared_ptr, when connection is closed, decreasing the refcount and set the connection status to closed.

Xu Pan
  • 310
  • 3
  • 12