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

This is a discussion on the old debugger-fixes-your-problem-but-does-say-a-peep trick within the C++ Programming forums, part of the General Programming Boards category; I discovered why a program of mine has been crashing Code: Char* ret = new Char[size]; At first I thought ...

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

    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?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,605
    I'm using dev btw
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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. Debugging a rare / unreproducible bug..
    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
    1,605
    No no, the actual call to new is what's crashing, not the writing to of it's returned elements.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04:47 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM

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