Application throwing a generic error.  
Author Message
David S. Anderson





PostPosted: Visual C++ General, Application throwing a generic error. Top

I'm developing a TcpServer in C++/CLI. I have everything coded. Now the problem is that when I want to close the connection to a client, it throws a generic error. A NullReferenceException. I am storing each client in an array. Here is the code and God bless you if you can help me fix this.


Visual C++7  
 
 
Gordon Hogenson - MSFT





PostPosted: Visual C++ General, Application throwing a generic error. Top

The for each loop where you're processing the close command :

for each (Socket ^client in clientarr)

is actually looping over all 100 elements in the array, most of which are null. That's because for each doesn't know anything about the validity of the values of the array elements, it just sees 100 handles that you've allocated, so then it blows up when you access the Remo****dPoint property. I found that adding a check for null fixes the code:

if (clientarr[index] == nullptr) continue;