Weird errors

This is a discussion on Weird errors within the C++ Programming forums, part of the General Programming Boards category; Hi, i'm building a application and at a certain point, i get the weirdest runtime errors: for example: Code: try{ ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    30

    Weird errors

    Hi, i'm building a application and at a certain point, i get the weirdest runtime errors:

    for example:

    Code:
    		try{
    			bufptr += my_strcpy(target->ServerName, bufptr);
    			bufptr += my_strcpy(target->MapName, bufptr);
    			bufptr += my_strcpy(target->Game, bufptr);
    			bufptr += my_strcpy(target->GameType, bufptr);
    			bufptr += 2; //Skip appid because it is no use to us
    			target->PlayerCount = static_cast<unsigned short>(*bufptr++);
    			target->MaxPlayers = static_cast<unsigned short>(*bufptr++);
    			bufptr+=3;
    		}
    		catch (std::bad_alloc&){
    			return -1;
    		}
    		catch(void *){
    			return -1;
    		}
    for some or other reason, the app jumps to the first catch block while executing this line:
    Code:
    bufptr+=3;
    Code:
    009936F5  mov         eax,dword ptr [ebp-48h] 
    009936F8  add         eax,3 
    009936FB  mov         dword ptr [ebp-48h],eax 
    009936FE  jmp         $LN19 (99372Ch)
    the jump leads straight to the catch block so why is it there :S
    Some other strange things are that whether i place some other code at that location. the catch block kicks in at that code.

    you might think that with buffer+=3, i have a overflow but thats not. bufptr points to the very beginning of a large large array of chars named buffer.


    my_strcpy definition:
    Code:
    	inline int my_strcpy(char* source, char* target){
    		int counter(0);
    		while (*target!=0){
    			*source++=*target++;
    			++counter;
    		}
    		*source = 0;
    		return counter+1;
    	}
    edit

    yet another weird thing. the function wont return with the return -1 cmd in the catch block. it just continues :S
    Last edited by DV64h; 10-14-2006 at 08:04 AM.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Could you please post your code?
    I suppose you just made a very stupid mistake in your code.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Are you sure there is anything that might throw an error in my_strcpy (writing to random memory being undefined behaviour)? Other than that, my_strcpy doesn't look very safe. Why make your own if there are better ones available.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Why does my_strcpy copy from target to source? Normally it would be the other way around.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Daved
    Why does my_strcpy copy from target to source? Normally it would be the other way around.
    Because of this:
    Code:
    *source++=*target++;
    You copy a function from somewhere and you don't even understand it.
    And still, you don't want to use the standard ways to do it.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You misunderstood the question. I know how the function copies from target to source. The question was why does it not copy from source to target. Variable names should show intent, and when the code does the opposite of what the variable names indicate, it is either a mistake or poor design. Perhaps in this case that is the solution to the problem.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    30
    Quote Originally Posted by anon
    Are you sure there is anything that might throw an error in my_strcpy (writing to random memory being undefined behaviour)? Other than that, my_strcpy doesn't look very safe. Why make your own if there are better ones available.

    thats the thing, i could use standard defined ones but that would be much slower, this is a strlen and strcpy in one and the whole application is based on speed.
    Personally, i believe this is a compile bug, because the Assembly is just messed up. Posting the code wont be relevant, because everything is very ok there edit, and the naming.... hence, its ugly but neither that is relevant
    Last edited by DV64h; 10-14-2006 at 10:42 AM.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Couldn't you just return ... hmm ... target+1 instead of using an additional counter and lots of additions if you were so interested in speed?

    You are also catching std::bad_alloc&, but you are not allocating any memory here, so what's the point?

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    30
    well, lets just say that is the point. why in *** name does he skips to the catch blocks and totally ignores it. hence why is the jump to the catch block anyway? there aint a error or what. and please explain what you mean with the target+1 part.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Daved
    You misunderstood the question. I know how the function copies from target to source. The question was why does it not copy from source to target. Variable names should show intent, and when the code does the opposite of what the variable names indicate, it is either a mistake or poor design. Perhaps in this case that is the solution to the problem.
    Sorry, I didn't notice it was you...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I don't usually pay much attention to the assembly, but when I step through code in the debugger it often takes me to the end of the catch block after the last statement in the try. Perhaps it is there that it cleans up the local variables. Is anything actually happening that is wrong? The title sas "weird errors". What errors?

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    30
    Quote Originally Posted by Daved
    I don't usually pay much attention to the assembly, but when I step through code in the debugger it often takes me to the end of the catch block after the last statement in the try. Perhaps it is there that it cleans up the local variables. Is anything actually happening that is wrong? The title sas "weird errors". What errors?
    runtime errors, sometimes the esp just points to a random adress, or some data is totally corrupted and the other threads dont even share the same object.
    and with the debugger, it dont just point to the end of the catch block it just enters it and steps over every statement without doing a thing. im using VC8

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    What do you mean by esp? Does your program actually get errors like access violations?

    Don't always trust what you see in the debugger, it doesn't always map exactly to what is going on.

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    30
    esp is instruction pointer right? it points to the next instruction to execute, and yes with the data i get acces violations

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The ESP register is the stack pointer: it points to the top of the stack, ie it contains the address of the top of the stack. Whenever anything is pushed on the stack the ESP register automatically changes.
    .....
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 02:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Weird Errors in VS 2003
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 06:16 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21