Skip to main content
gamedev.net
🔒 Locked

heap trashing?

Started by jaap 4 replies 900+ views
Original Post
jaap
jaap
I've got this really weird problem, which I can't wrap my head around: I allocate a few winsock SOCKET wrappers as follows: Socket** stock = new Socket*[stocksize]; for (int x = 0; x < stocksize; x++) stock[x] = new jws::Socket(); And I remove them all in a similar fashion. for (int x = 0; x < stocksize; x++) if (stock[x]) delete stock[x]; The problem is that I have to explicitly delete all of them or else MSVS tells me the heap is corrupted. so this: for (int x = 0; x < stocksize-1; x++) stock[x] = new jws::Socket(); gives errors as during wrapup. But the above ONLY happens if I use the wrapped socket in a Winsock API. If I just allocate the Sockets, nothing bad happens. More precisely, it only seems to happen when I run the Socket object through the following function sometime during the execution: AcceptEx(socket->_getWinSocket(), sock->_getWinSocket(), buf, 0, sizeof(sockaddr_in) + 32, sizeof(sockaddr_in) + 32, &recv, (&ola.ol)); Where _getWinsocket() returns the wrapped SOCKET object/handle. Am I allocating the sockets wrong or something? I really can't figure it out. Maybe I'm missing something... Using the latest MSVS 2005.
Fruny
Fruny
Do you properly call delete[] stock; ?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jaap
jaap
Yes I deleted the pointed array like a good boy. :)
Palidine
Palidine
Quote:

for (int x = 0; x < stocksize; x++)
if (stock[x]) delete stock[x];

The problem is that I have to explicitly delete all of them or else MSVS tells me the heap is corrupted.


This part of your post is confusing. It sounds like the "problem" is that you have to explicity call that code? Well, that's what you have to do, it's the way of the language. You can't leave out that code or it will properly barf.

Am I reading your "problem" right?

-me
Evil Steve
Evil Steve
Wait a moment - You get an error saying the heap is corrupted if you don't delete all of stock? When do you get this error? When the program exits?
jaap
jaap
I think I solved the problem, kinda. I'm sorry to have waisted everybody's time and I want to thank everybody who took some of their time to help me.
Ok, here's the skinny:

The winsock AcceptEx() function used in the OverlappedAccept object takes a pointer to a struct (on the heap) for input. It turned out that when the program finishes, windows "rolls up" all winsock stuff you have still left "unfinished" and aparently it wants the forementioned struct to be still standing when it does.

The reason that I never got this before is because I always clean up my sockets and my OverlappedAccept object, or I don't clean up everything (when I'm testing/lazy). It turns out that it only occurs if the OverlappedAccept object is destructed (thus that struct passed to AcceptEx() is trashed) and the socket is still unclosed.

This is my best guess in how this "error" comes to be. The thing is that when I run the .exe outside the MSVS I don't get any error whatsoever. I'll just clean up after myself like a good boy and I think I'll be fine.

Thanks again everybody.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.