"new" is strange

This is a discussion on "new" is strange within the C++ Programming forums, part of the General Programming Boards category; Hi I'm writing a game engine, and when I was going to send it to a friend I compiled it ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92

    "new" is strange

    Hi
    I'm writing a game engine, and when I was going to send it to a friend I compiled it as release and ran it. But it crashed and I spend like 4 hours looking for the problem, I found that whenever I call new it crashes.

    The strange thing is that it only crashes when I run it from the exe-file, it runs perfect in MSVC.NET and even when I tried to debug it with Ollydbg. An examle of where it crashes:

    Code:
    tChunk* tempChunk = new tChunk;
    tChunk is a struct. Everything has been working perfectly before, and I haven't changed anything on this code.

    Anyone knows what causes this strange crash?

    Thanks in advande

    The Wazaa

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    I really doubt it is that call to new. More likely accessing invalid memory or something along those lines.
    Woop?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Well, it crashes on the line that I typed up there. I tried this:
    Code:
     MessageBox(NULL, "Calling new on tempChunk", "Info", MB_OK);
     tChunk* tempChunk = new tChunk;
     MessageBox(NULL, "Worked, reading now", "Info", MB_OK);
    It shows the first messageBox, and then crashes so I guess that it must be that call.

    Thanks for your reply

    The Wazaa

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Does the tChunk constructor do anything peculiar?

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Well, it doesn't even have a constructor. This line also crashes:
    Code:
    int* version = new int;
    All this code is inside a class called TdsLoader. I call this function from a pointer to a ' = new TdsLoader' might it be that? I have used that for long now so I don't think that it is the problem.

    The Wazaa

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    It shows the first messageBox, and then crashes so I guess that it must be that call.
    Check recent memory accesses(both around the crash and by execution order) for fence post errors, delete[] when delete was needed, etc. Your constructor, if you have one, could also be causing the crash; check it also.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Did I get this right: the release version crashes, but the debug version doesn't?
    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

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    It can't be the constructors fault, since I don't have one. And everything has worked wonderful earlier. This is the message it shows (when I debug it after it has crashed):

    Unhandled exception at 0x7c93426d in GLengine.exe: 0xC0000005: Access violation reading location 0x758c0bbe.

    Here is a screenshot of the call stack:
    http://e43.sag.karlstad.se/it18/screen.JPG

    As you see it crashes in ntdll.dll, from a call to operator new, which calls some allocs.

    Thanks for your replies

    The Wazaa

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You haven't answered my question.

    Does the stack trace come from the crashing release version? If so, why is a debug allocator called?
    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

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Oh you must have posted while I wrote my post, both the realease and the debug version crashes when I run the exe-file. But both works in MSVC.NET and in Ollydbg, which confuses me a bit. I've tried every change I could think of with both files so I guess that debug was the last build I used.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Have you tried a clean rebuild? (Remove the Debug and Release subdirectories of your project and then rebuild.)
    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

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,660
    Wazaa, the most likely cause of your crash is some code BEFORE the line where the crash occurs. It is, for example, possible to see this;
    Code:
    int main()
    {
          int *x = (int *)42; 
          *x = 1000;                    // the undefined behaviour is here
          int *y = new int[20];    //   but the crash is here
    }
    The common belief that an invalid pointer operation ALWAYS results in an immediate crash is wrong. The actual situation is that undefined behaviour means anything can happen; it just happens that a common result is an immediate crash, but it doesn't have to be. And if the effect is to corrupt the tables used internally by malloc() or operator new(), the result of an invalid operation can be a crash in a future call to malloc()/free() or to operator new/delete.

    The symptom of the program crashing in release mode but not debug mode strengthens my suspicion that the problem is code executed SOMEWHERE BEFORE the offending line: one difference between release mode and debug mode is that memory (such as that used internally by operator new and delete) can be structured differently. Which means the memory that is being corrupted is used differently in release and debug modes.

    In short, look for pointer molestation in code that is executed BEFORE the place where the crash occurs. Possible causes, as I've said, are any invalid operation involving a pointer (eg dereferencing a NULL, falling off the end of an array, etc etc). And, when hunting for the problem, I'm not suggesting looking a couple of lines back: look in ALL CODE executed between program startup and the crash.
    Last edited by grumpy; 03-05-2006 at 03:46 PM.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Did not work, still the same error :-/

    EDIT: I see, okay going to try that tomorrow. Thanks.

    The Wazaa

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    can you construct a tChunk on the stack?

    tChunk doesn't happen to have a huge array or something in it?

    have you tried the following?
    Code:
    try
    {
        tChunk* pChunk = new tChunk
    }
    catch (std::bad_alloc &)
    {
        cout << "eeek! out of memory!";
    }
    [edit]BTW lose the 't' from tChunk. This is c++ no need for a 'type' prefix.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  15. #15
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    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);
    	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);
    	}
    
    
    	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);
    
    
    	fclose(file);
    
    	TdsLoader *modelLoader = new TdsLoader;       // <--- This don't, it crashes
    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.

    Thanks for your replies

    The Wazaa

Page 1 of 2 12 LastLast
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, 09:47 AM
  2. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 11:21 AM
  3. Very strange bug...
    By JaWiB in forum Tech Board
    Replies: 6
    Last Post: 04-27-2003, 02:56 PM
  4. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 06:31 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM

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