RecvFrom call causing unhandled exceptions

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I apologize for all these threads but this one has me truly stumped now...my program is going surprisingly well actually, until this point. Everything is compiling fine, but...

Whenever I put in this call, VS says I have unhandled exceptions in ntdll.dll. When I comment it out there are no exceptions at all.

recvfrom(sd, RecvBuf, BufLen, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);

Yields:

Unhandled exception at 0x7c901230 (ntdll.dll) in bf2gui.exe: User breakpoint.
I don't know what it means by user breakpoint...I don't have any breakpoints in this program at all.

Then it points at the MessageBox function with a green arrow, or if I comment that out, it points to the line below it, activethreads--;. If both are commented it points to somewhere in a windows header file...

I have successfully used recv before but not recvfrom...I don't know why recvfrom isn't working...

Using Ethereal I can tell it is sending the query data I want it to, and the response seems correct. But I need to get that response in to a string without it failing somehow.

BTW, doing a char *buff=NULL;
buff=(char*)malloc(4096+1);
...
free(buff);

yields the same error in execution.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Thanks...it works...one more thing:

Is the following code correct/safe? It seems it works even without the realloc function, but it's safe to use realloc, right? "server" is a custom structure.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
server[0] (servers0) could be possibly lost depending on memory usage.

You would be best to allocate a servers1 pointer of server*2;

then copy servers0 into the new memory structure servers1.

Delete the original servers0.

This way you are guarenteed to have the data.

If there is no memory available to "extend" severs0, then the realloc will fail.

Also, make sure that you test the result of malloc/realloc before use.
 

Templeton

Senior member
Oct 9, 1999
467
0
0
Originally posted by: EagleKeeper
server[0] (servers0) could be possibly lost depending on memory usage.

You would be best to allocate a servers1 pointer of server*2;

then copy servers0 into the new memory structure servers1.

Delete the original servers0.

This way you are guarenteed to have the data.

If there is no memory available to "extend" severs0, then the realloc will fail.

Also, make sure that you test the result of malloc/realloc before use.

You sure it's possible to lose previous data on a call to realloc? My understanding was that if there isn't enough free memory, it'll return NULL and leave the original location untouched. This behavior would make the most sense if you think about what realloc actually has to do when additional space requested:

1)Allocate a new chunk of memory
2)Copy contents of old mem to new mem
3)Free old mem

If step #1 fails, doesn't seem to be any reason to touch old memory.
 

Templeton

Senior member
Oct 9, 1999
467
0
0
for your new problem, c requires all variables be defined at beginning of block, before any assignments, function calls, etc..

define newItem at begginning of function.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Templeton
Originally posted by: EagleKeeper
server[0] (servers0) could be possibly lost depending on memory usage.

You would be best to allocate a servers1 pointer of server*2;

then copy servers0 into the new memory structure servers1.

Delete the original servers0.

This way you are guarenteed to have the data.

If there is no memory available to "extend" severs0, then the realloc will fail.

Also, make sure that you test the result of malloc/realloc before use.

You sure it's possible to lose previous data on a call to realloc? My understanding was that if there isn't enough free memory, it'll return NULL and leave the original location untouched. This behavior would make the most sense if you think about what realloc actually has to do when additional space requested:

1)Allocate a new chunk of memory
2)Copy contents of old mem to new mem
3)Free old mem

If step #1 fails, doesn't seem to be any reason to touch old memory.

Some implimenations may attempt to actually extend the memory area. That is a concern that I was attempting to point out.

Either way, the code had no safety checks.

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Well actually that code had nothing to do with my second problem. The second problem seems very mysterious. Seems like whenever I try to declare a variable I get some weird syntax error that comes out of no where. It's someone else's code I'm basically making a GUI for. Also more weird weird things like I include commctrl.h and then all of a sudden I get syntax errors off the charts. If I don't include it, it's fine. It seems like I have a broken #DEFINE or something. But there's a million defines in this guy's code, making it virtually impossible to track down where this is coming from. I noticed there are a lot of differences between C and C++. This guy wrote his program in C, and I can't do similar stuff in C++ without typecasting. For example C is more forgiving in terms of arguments, like I can do unsigned char in place of char in C, but not in C++. Maybe this has something to do with my issues.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Code maintenance is hell. Without documentation it is nearly impossible.

However, once you have had to do it; you may then never repeat such a instance of poor judgement again.

If you are having problem trying to declare a variable and you think that it is the #DEFINE issue, then track down the type that you are using and figure out which #DEFINE is shafting you.

Iterative process; with no shorts cuts.

Just be glad that you are not submitting punch cards into a batch processing center for compilation runs.
 
Sep 29, 2004
18,656
67
91
Originally posted by: xtknight
1st question answered but here's another:

(in one big main function, these aren't in any loops, just right in the function, and no redeclarations)

servers = 0;
char newItem[22];

error C2143: syntax error : missing ';' before 'type' (points to line 2)

So what is this supposed to mean? :confused: declaring newItem as int fails similarly.

You never said what servers is.

char, int, double. etc

C/C++ compiler errors usually suck. If the line the error is supposed to be on looks correct, always work backwards. I've found errors 100 lines off of where hte compiler reported the error.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
servers is a u_int (typedef unsigned integer). commenting servers=0 out does not change anything.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Look for a $undef statement for the u_int. That may be causing the problem.

As a quick test, in the code replace the u_int with unsigned int
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I noticed another oddity. It seems anything I mouseover always says "int max". Whether it be right over fprintf, inet_ntoa, my own char variable, or another one of my functions, I see it. No idea what that means...maybe it's a bug in Intellisense. max is a u_char, not an int... while I understand char can be interchangeable with unsigned __int8 because both have a max of 0-255, but this int is a default 32-bit.

Search found 17 #undefs but none were related to u_int in any way.

There is definitely something odd about this file... I tried replicating the code in it twice in my own C++ program, I swear I got everything necessary being an exact mirror, and the code just refused to work. Then in C++ I had to change some look-up table's definition to 257 instead of 256, which made no sense...

I doubt anyone would want to bother, but if you want to try and fix it or want a challenge, the source code is all free and I'd gladly send it. Most of it is someone else's freely available console program, and I tried to convert it to a Win32 GUI program. All of it is in C right now, no C++.
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
Most C compilers will not allow you to declare another variable within a block once you have started the code for the block. You need to move your declaration for newItem to the top of your function with all of your other variable declarations. Alternatively you include the declaration and the code that uses it into a new code block using {}.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: Cerebus451
Most C compilers will not allow you to declare another variable within a block once you have started the code for the block. You need to move your declaration for newItem to the top of your function with all of your other variable declarations. Alternatively you include the declaration and the code that uses it into a new code block using {}.

Ahh, ok, thanks...

I have decided to reassemble my project anyway but I'll keep that in mind in the future.