"new" is strange

This is a discussion on "new" is strange within the C++ Programming forums, part of the General Programming Boards category; The working path is different between direct runs and runs from within the IDE. Have you considered that the file ...

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The working path is different between direct runs and runs from within the IDE. Have you considered that the file simply might not be found? In other words, have you checked the return value of fopen?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    It finds the file, and it reads it correctly. But it still crashes. Oh and the release version crashes on dummy3 the debug version crashes on the next "new".

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    Quote Originally Posted by The Wazaa
    Hi, I have checked backwards. And as you said there was an error before that. Here is the area in which the error occures ( I think ):
    Code:
    fread(&temp, sizeof(int), 1, file);
    This line should be OK, if temp is of type int.
    Quote Originally Posted by The Wazaa
    Code:
    	if (temp > 255 || temp < 1)
    	{
    		MessageBox(NULL, "Invalid mapname", "Error", MB_OK);
    		return;
    	}
    	try
    	{
    		worldFileName = new char[temp];      // This line makes "new" go crazy...
    	}
    	catch (std::bad_alloc &)
    	{
    		MessageBox(NULL, "AIIEEE, out of memory", "Error", MB_OK);
    	}
    Unless your system is running out of memory, you're just seeing another symptom of your original problem.

    I would bet that the real problem is in code executed even earlier than this code.

    As a rough rule, there are only two things that can make operator new "go crazy": running out of memory (in which case an bad_alloc exception will be thrown and caught). If that's not the case, then your most likely offender is code that executed somewhere before the "crazy" behaviour became apparent.
    Quote Originally Posted by The Wazaa
    Code:
    	readStr(temp, worldFileName, file);		
    
    	memset(worldFileName+temp, 0, strlen(worldFileName) - temp);
    	MessageBox(NULL, worldFileName, "Info", MB_OK);
    
    	WE_ERROR("Testing dummy3", EH_MBERROR, mainLog);
    	int *dummy3 = new int;           // <---- This new works
    	WE_ERROR("Dummy3 okay", EH_MBERROR, mainLog);
    The fact that this "new works" means nothing. A problem has occurred earlier, and you need to find the cause of that.

    As a rough rule, the symptoms of a problem cannot occur before the cause.

    Quote Originally Posted by The Wazaa
    Code:
    	fclose(file);
    
    	TdsLoader *modelLoader = new TdsLoader;       // <--- This don't, it crashes
    This is just a second symptom of your underlying problem. Which, I repeat, will be in code executed before this fragment.
    Quote Originally Posted by The Wazaa
    This code is from a place where I try to read a file. I know that the file is correct since it works in debuggers. But when I run the exe, something strange seems to happen with all the strings and it looses them.
    You are mistaken in your assessment that the "file is correct since it works in debuggers".

    Code working correctly in debuggers, but not in production, is a symptom of a serious problem in your code (unless you have encountered a problem in the debugger or compiler: that is possible, but highly unlikely in your case).

    The place you need to look is in code that is executed before the latest snippet you've given.

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Since the problem could be anywhere (as grumpy said), you need to basically post the whole code for people to get a good look at all the things which could be going wrong.

    If the project is still pretty small, then post it 'as is'.

    If it's large, then you need to produce a smaller runnable example which still shows the problem.
    a) make a backup / clone of the project
    b) remove code which hasn't run yet.
    c) experiment with commenting out code which has run, but which doesn't change the crash.

    Hopefully, when you've done that, you'll have a much smaller (and easier for us to examine) example program to diagnose.

    Often, just doing this will lead you to the answer you seek, as you eliminate possibilities.

  5. #20
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Luckily this was almost in the beginning of the whole project. A window is created before it, but I switched the location of that initialisation so it is created after but the program still crashes.
    This is all the code that runs before the crash, and a bit after it:

    Code:
    // Check below
    Thanks for all your replies

    The Wazaa
    Last edited by The Wazaa; 03-06-2006 at 11:33 AM.

  6. #21
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't exactly see what these two lines are trying to accomplish:
    Code:
    memset(models[i].fileName+temp, 0, strlen(models[i].fileName) - temp);
    //...
    memset(worldFileName+temp, 0, strlen(worldFileName) - temp);
    In both cases, temp is the size of the buffer that you are using memset on, which means you are most likely writing past the bounds of your array.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #22
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Well, of some strange reason my strings get longer than I want, so I simply writes over the memory after it. Are you sure this causes the error? I thought that memset did not care about whether it writes outside the boundry of an array. And, if the length of the string would be as big as I wanted, it would not overwrite anything so I don't think that it is the problem. Its just an ugly way to solve that a lot of wierd characters seem to get into my string

    Thanks for your reply

    The Wazaa

  8. #23
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You should never write outside the bounds of your array (unless you're writing a buffer overflow exploit)

    If your strings don't hold the data you expect to see, then it's a sign that there is something else wrong in your code (in readStr maybe?)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #24
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Oh, I think I forgot to post readStr. And the characters that I speak about is in the string from the moment I create it. Check above for the readStr function.

  10. #25
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    But that is weird the way it runs from within the compiler. Maybe the working directories are set up wrong. With my compiler, my program always works when run inside the compiler, because the compiler sets the right working directories, but it usually crashes when I run the actual exectuable because it can't find the file that I'm trying to load.

  11. #26
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > memset(models[i].fileName+temp, 0, strlen(models[i].fileName) - temp);
    If you want a \0 terminator, just do
    models[i].fileName[temp] = '\0';

    Your strlen() assumes there is a \0 there to begin with, and if it's not there you end up with v.large values of strlen(), resulting in very large values of memory being cleared.

    I could have sworn I asked you to post something which compiles and runs, yet you just posted random snippets all the same.
    If you don't post full compilable code to such problems, expect half-assed answers and guesses in response.

  12. #27
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    It works now

    It actually was the strings, which I have been trying to get Null-terminated before in the right way but nothing worked. Thanks a lot to everyone who posted here

    The GL engine http://e43.sag.karlstad.se/it18/Skicka.zip if you want to see it

    The Wazaa

    ~* Old post *~
    Sorry, I've been breaking my whole program down now and only the needed code is included, at least I think so
    And here is a the complete source:

    EDIT: removed
    That is all code I have and it runs perfect in the debugger but the exe crashes.

    Thanks for your replies

    The Wazaa
    Last edited by The Wazaa; 03-06-2006 at 12:27 PM.

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

Similar Threads

  1. Need help with a strange for-loop problem
    By jasperleeabc in forum C++ Programming
    Replies: 4
    Last Post: 05-29-2009, 08:47 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  3. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 01:56 PM
  4. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 05:31 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 03:00 PM

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