error when return 0; in int main()w

This is a discussion on error when return 0; in int main()w within the C++ Programming forums, part of the General Programming Boards category; I get the following error, but only when I return 0: *** glibc detected *** entropy: double free or corruption ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    106

    error when return 0; in int main()w

    I get the following error, but only when I return 0:

    *** glibc detected *** entropy: double free or corruption (!prev): 0x08775e10 ***

    no error will pop up until my main function returns 0. From my research this is an error that you get when the program is having trouble deallocating memory. I am using some vectors in my main function, but I'm not sure if it can be due to that. I made sure to clear clear all my vectors before the end of the program but that didn't do the trick. I'm sofa king confused!!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by elninio View Post
    I get the following error, but only when I return 0:

    *** glibc detected *** entropy: double free or corruption (!prev): 0x08775e10 ***

    no error will pop up until my main function returns 0. From my research this is an error that you get when the program is having trouble deallocating memory. I am using some vectors in my main function, but I'm not sure if it can be due to that. I made sure to clear clear all my vectors before the end of the program but that didn't do the trick. I'm sofa king confused!!
    Even if clearing the vectors made the error go away, it would not resolve the problem. You are corrupting the heap in some manner. Are you dynamically allocating memory, or only using vectors? Either way, you are accessing (writing to) memory beyond limits somewhere.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by brewbuck View Post
    Even if clearing the vectors made the error go away, it would not resolve the problem. You are corrupting the heap in some manner. Are you dynamically allocating memory, or only using vectors? Either way, you are accessing (writing to) memory beyond limits somewhere.
    I've tried dyamicall, then I've tried setting a vector size, and resizing the vector everytime the loop starts; something like this.

    for(
    vect.resize()
    for (
    vect[i]++
    )
    vect.clear()
    )

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You propably have to post the REAL code, not some "something like this" - "something like this" is fine for explaining how something should be done, but when it comes to bug-finding, it is often subtle details that are causing the problem, not the overall approach (although that is of course also a potential problem).

    --
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not Linux-specific, moving.
    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

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Code:
    map<string, int> Fmap;
    vector<map<string,int> > kFmap;
    
    cout << "... Indexing feature-values ... ";
    for (unsigned int k = 0; k < spec.features.size(); k++){
      //Fmap.resize(spec.features.size());
      for (int i = 3; i <= argc; i++){
        dataset_loader loader(argv[i]);
        Fmap[namesmap[i]] = spec.features[k].load(loader);
      }
      kFmap[k] = Fmap;
      Fmap.empty();
    }
    cout << "OK" << endl;
    this one gave a seg fault

    have also tried:

    Code:
    map<string, int> Fmap;
    vector<map<string,int> > kFmap;
    
    cout << "... Indexing feature-values ... ";
    for (unsigned int k = 0; k < spec.features.size(); k++){
      //Fmap.resize(spec.features.size());
      for (int i = 3; i <= argc; i++){
        dataset_loader loader(argv[i]);
        Fmap[namesmap[i]] = spec.features[k].load(loader);
      }
      kFmap.push_back(Fmap);
      Fmap.empty();
    }
    cout << "OK" << endl;
    this one worked, but I'm afraid it has been done incorrectly, because after I execute a similar loop following this, my program breaks RIGHT AFTER the "cout << "OK" << endl; of the 2nd block of code posted ABOVE. Here is the THIRD block of code that follows the SECOND (the FIRST is no longer in the picture):

    [
    Code:
    // Index P(F) matrices
    cout << "... Indexing P(F) matrices ...";
    vector<vector<int> > kF;
    vector<int> F;
    for (unsigned int k = 0; k < spec.features.size(); k++){
      F.resize(spec.features[k].max_val());
      for (int i = 3; i <= argc; i++){ 
        F[kFmap[k][namesmap[i]]]++;
      }
      kF.push_back(F);
      F.clear();
      cout << "k = " << k << endl;
    }
    cout << "OK" << endl;
    	
    for (unsigned int i = 0; i < kF.size(); i++){
      for (unsigned int j = 0; j < kF[i].size(); j++){
        cout << kF[i][j] << " ";
      }
      cout << endl;
    }
    for this part of code, the program runs all of this code, but then gives me the *** glibc detected *** entropy: free(): invalid next size (normal): 0x0875ccb0 *** error. When i remove

    Code:
    for (unsigned int i = 0; i < kF.size(); i++){
      for (unsigned int j = 0; j < kF[i].size(); j++){
        cout << kF[i][j] << " ";
      }
      cout << endl;
    }
    from code the third code block, it runs about halfway (k = 32, but k=53 normally), and gives *** glibc detected *** entropy: munmap_chunk(): invalid pointer: 0x08752d88 ***
    Last edited by elninio; 09-17-2008 at 10:20 AM. Reason: formatting

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    A couple of tools
    http://valgrind.org/
    http://www.perens.com/works/software/ElectricFence/

    For example, if you link with the electric fence library, and run the result in the debugger, it should trap at the point you trash memory.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by Salem View Post
    A couple of tools
    http://valgrind.org/
    http://www.perens.com/works/software/ElectricFence/

    For example, if you link with the electric fence library, and run the result in the debugger, it should trap at the point you trash memory.
    thanks, which do you recommend? I'll try both but I'd like to hear your thoughts on them.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    malloc() resulting in a SegFault?!

    after consulting this thread, does the same case apply in my situation? I am not sure if the error is from trying to deallocate memory that has already been deallocated, or trying to acccess deallocated memory, or trying to access memory reserved by other programs

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    The problem is simply undefined behavior. Most likely you are thrashing memory, as pointed out.
    These tools will help you locate such things, memory errors.
    I can also recommend using Visual Studio's debugger. By default, it tends to trap some common errors such as writing beyond the end of an array (or you can use use the vector's at method).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    One thing that stands out is loops like this:

    Code:
    for (int i = 3; i <= argc; i++)
    If I'm not mistaken that would normally loop for one more argv than there is, passing NULL somewhere that might not expect it and/or i might get out of bounds for something.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by anon View Post
    One thing that stands out is loops like this:

    Code:
    for (int i = 3; i <= argc; i++)
    If I'm not mistaken that would normally loop for one more argv than there is, passing NULL somewhere that might not expect it and/or i might get out of bounds for something.
    argc is the number of arguments including the exe name. my file runs like this:

    filename specfile specfile file1 file 2 file 3... file n.

    therefore argc = n+3 , so i start at int i = 3, that is file 1.

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by elninio View Post
    argc is the number of arguments including the exe name. my file runs like this:

    filename specfile specfile file1 file 2 file 3... file n.

    therefore argc = n+3 , so i start at int i = 3, that is file 1.
    also i think if i was accessing an element that i was supposed to, wouldn't it give me seg faults instead of gtklib errors?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    anon is concerned about the loop condition, not the initial value of i. argv[argc] is a null pointer, so on the last iteration of the loop, accessing argv[i] would access argv[argc], which may not be what you intend. To avoid this, change the loop condition to i < argc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by laserlight View Post
    anon is concerned about the loop condition, not the initial value of i. argv[argc] is a null pointer, so on the last iteration of the loop, accessing argv[i] would access argv[argc], which may not be what you intend. To avoid this, change the loop condition to i < argc.
    wouldn't this not process the last file I pass as parameter? thats what i was refering to in my reply.

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

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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