Thread: Unkown Hang

  1. #16
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by Snafuist View Post
    Unlikely, but fopen() may call malloc(), which may produce very strange bugs (although I haven't seen malloc() doing infinite loops). What happens if you allocate a fixed size memory range which is large enough (5000 bytes?) to hold everything you'd put there?

    Greets,
    Philip
    That should not be his solution. He should fix the problem.

    And I thought that fopen() calling malloc() was a definite; what brings doubts? It is returning a FILE*, so obviously, it cannot return a local variable, rather it allocates the memory on the heap, using, of course, malloc().

  2. #17
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yes fopen() allocates storage on the heap but why should that stop the OP from getting a big chunk of memory for debugging purposes.

  3. #18
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by itCbitC View Post
    Yes fopen() allocates storage on the heap but why should that stop the OP from getting a big chunk of memory for debugging purposes.
    I do not know. I was simply clarifying the apparent doubt on whether fopen() used malloc().

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Replace the fopen call with a malloc call. Allocate a few megs of memory, and see if it makes it hang. If it doesn't, call free.

    You might also consider opening your log file ahead of time, and just leaving it open for the duration of the program.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by abraham2119 View Post
    I do not know. I was simply clarifying the apparent doubt on whether fopen() used malloc().
    gotcha!
    no worries, got confused with your post, so thanks for clarifying
    Last edited by itCbitC; 04-21-2009 at 05:01 PM.

  6. #21
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by abraham2119 View Post
    That should not be his solution. He should fix the problem.
    Of course! I suggested this naive technique in favor of using a debugger, because if malloc() fails, then the heap was most likely already trashed before that call. A debugger won't help much here. Simply allocating enough memory may give proof to the assumption that the hang is indeed caused by malloc().

    And I thought that fopen() calling malloc() was a definite; what brings doubts? It is returning a FILE*, so obviously, it cannot return a local variable, rather it allocates the memory on the heap, using, of course, malloc().
    What brings doubt is the fact that I didn't write malloc(). My manpages states that the "fopen(), fdopen() and freopen() functions may also fail and set errno for any of the errors specified for the routine malloc(3)", which doesn't imply that they are actually calling malloc() (or allocating memory at all). Imagine fopen() re-implementing the malloc() functionality, or having a fixed size buffer large enough to hold the maximum number of open FILEs. This may seem stupid, but I didn't want to make premature assumptions about the true nature of fopen().

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  7. #22
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Quote Originally Posted by quzah View Post
    You might also consider opening your log file ahead of time, and just leaving it open for the duration of the program.
    That really isn't an option. For the environment the program will be running in, there could be unexpected losses of power that would then cause me to lose all my diagnostic log.

    So are you suggesting that I replace the fopen() with (FILE *) malloc(5000); and see what happens then?

  8. #23
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by Bladactania View Post
    So are you suggesting that I replace the fopen() with (FILE *) malloc(5000); and see what happens then?
    No, I'm suggesting to replace every malloc(<expression>) in your code with malloc(<some constant value that is guaranteed to be large enough to hold whatever you want to store there, e.g. 1MB of memory>).

    In my opinion, the hang is caused by a trashed heap due to allocating less memory than is actually needed. In order to give evidence to this assumption, it's a good idea to check whether the hang disappears if you simply allocate enough memory. The problem with a trashed heap is that strange behavior may occur only after the next call to malloc(), which may be perfectly fine in itself (which I suppose fopen()'s malloc() to be).

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Snafuist View Post
    No, I'm suggesting to replace every malloc(<expression>) in your code with malloc(<some constant value that is guaranteed to be large enough to hold whatever you want to store there, e.g. 1MB of memory>).

    In my opinion, the hang is caused by a trashed heap due to allocating less memory than is actually needed. In order to give evidence to this assumption, it's a good idea to check whether the hang disappears if you simply allocate enough memory. The problem with a trashed heap is that strange behavior may occur only after the next call to malloc(), which may be perfectly fine in itself (which I suppose fopen()'s malloc() to be).

    Greets,
    Philip
    Quite often, replacing "malloc(x)" with "malloc(5*x)" will show that it's this type of problem, as Philip says.

    Since this is (or at least appears to be) DOS, I expect allocating 1MB may not work very well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Ok, now I've actually been reading your code and I think I know what causes the hang:

    Code:
      char *sTemp = (char*) malloc((sizeof(char) * strlen(s1)) + (sizeof(char) * strlen(s2)));
    
      strcpy(sTemp, s1);
      strcat(sTemp, s2);
    You need to allocate one more byte for the terminating '\0' or else strcat() will write to memory that hasn't been allocated previously:

    Code:
      char *sTemp = malloc(strlen(s1) + strlen(s2) + 1);
    Note that sizeof(char) == 1 and that you don't need to cast void* in assignments, which makes it slightly more readable.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  11. #26
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Note that sizeof(char) == 1 and that you don't need to cast void* in assignments, which makes it slightly more readable.
    I understood it was good programming practice to always explicitly cast mallocs (or other returned void pointers for that matter). Same thing for the sizeof(char).


    I will try your suggestion of adding the one extra byte though... Good catch!

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Casting malloc is NOT good practice in my view - malloc returns a void pointer, which can be converted to any other pointer without a cast [in C, rules are different in C++]. If you cast it, you will not see that you've forgot to include <stdilib.h> which provides the prototype for malloc. If we then have one of those ABI's where a pointer is returned in a different register than integer values, the compiler will actually generate code that uses the wrong register.

    sizeof(char) is GUARANTEED to be 1. Multiplying by 1 is pointless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I was not aware that char was guaranteed to be 1.

    Philip - I tried your suggestion and it appears to be working thus far. Many thanks for your excellent catch. I'd never even looked over that function as a possible source of the problem!

  14. #29
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What platform are you working on? IMO the OS should be able to catch this exception at runtime and abend the program with a segv.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    What platform are you working on? IMO the OS should be able to catch this exception at runtime and abend the program with a segv.
    It is DOS (or something close to DOS), I'm 99% sure of that - and it doesn't have memory protection, so no segv.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hang man program hanging
    By pieisgood in forum C++ Programming
    Replies: 14
    Last Post: 02-21-2009, 06:08 PM
  2. C# and the WebClient class - application hang?
    By Devils Child in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-09-2009, 11:24 AM
  3. How to get an unkown hWnd
    By Xzyx987X in forum Windows Programming
    Replies: 1
    Last Post: 11-26-2003, 03:29 PM
  4. storage size of regs is unkown
    By Vertex34 in forum C Programming
    Replies: 5
    Last Post: 11-04-2003, 10:17 AM
  5. Reading files with an unkown amount of characters
    By Zahl in forum C++ Programming
    Replies: 13
    Last Post: 10-10-2002, 02:04 PM