Thread: the old debugger-fixes-your-problem-but-does-say-a-peep trick

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158

    the old debugger-fixes-your-problem-but-does-say-a-peep trick

    I discovered why a program of mine has been crashing
    Code:
    Char* ret = new Char[size];
    At first I thought size was overflowing the memory, but my showHex func reveals that it's only 14. A memory leak I was thinking, but the line
    Code:
    Char ret[140];
    works just fine in it's place, and it's allocating 10 times the space.

    So time to use the debugger I thought, but when I run it through the debugger, NOTHING happens, go back to normal build, it crashes again. Somehow the debugger is fixing this problem, but it doesn't report any error or warning of any kind.

    What going on?

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I'm using dev btw

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Who knows? It could be anything. I'm betting on a buffer overrun, though, if allocating more space works.

    What if you still use dynamic memory allocation, but get more space than you need?
    Code:
    Char* ret = new Char[size * 10];
    This thread is turning out to be pretty useful. http://cboard.cprogramming.com/showthread.php?t=105440
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    No no, the actual call to new is what's crashing, not the writing to of it's returned elements.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Do you try and catch std::bad_alloc after the new statement? Should throw an exception of the type bad_alloc if it fails? I think...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > No no, the actual call to new is what's crashing
    In which case, the problem is somewhere else.

    Some other user of allocated memory is doing something wrong, say
    - use after free
    - double free
    - buffer overrun (or underrun)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Post the code for the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Visual c++ .NET Debugger problem
    By CartoonLarry in forum Windows Programming
    Replies: 4
    Last Post: 09-23-2004, 11:08 PM
  3. Problem with MSVC debugger - can't get it to break?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 04-07-2004, 05:22 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM