Thread: Damage error with clearing a vector

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Damage error with clearing a vector

    Hi,
    I have a function that fills a vector. It can be run multiple times during the program, but I don't want it to keep filling the vector as many times as it is run. So I put in a check:
    Code:
         if((FAD.vectDL.empty()) == 0)
    	      FAD.vectDL.clear();
    FAD is a global variable because it is accessed by a bunch of functions. .empty() returns 0 if there is somehting in the vector, but .clear() causes a run-time assertion error( Damage after Normal Block (#112)....).

    Can I not clear a global vector? How would I get rid of this error?
    I tried this without other functions that use the vector. I just ran this function twice in a row.
    Last edited by earth_angel; 08-25-2005 at 08:54 AM.
    Everything is relative...

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    if( !vector.empty() )
    {
         vector.clear();
    }

    where vector is replaced by the name of your vector

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you should consider going from a global variable,
    to passing if by pointer / address / refrence to the
    function

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by earth_angel
    Hi,
    Code:
         if((FAD.vectDL.empty()) == 0)
    	      FAD.vectDL.clear();
    FAD is a global variable because it is accessed by a bunch of functions. .empty() returns 0 if there is somehting in the vector, but .clear() causes a run-time assertion error( Damage after Normal Block (#112)....).
    Something else is going on in your code. Memory errors often blow up in unexpected places. The first step is to check the destructor (if you wrote it) for the object that vectDL contains. Next check for buffer overruns/underruns elsewhere in the program.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is no reason to check for empty before clearing the vector.

    You are always allowed to clear a vector, anonytmouse is correct that the problem is somewhere else in the code.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Code:
    if( !vector.empty() )
    {
         vector.clear();
    }
    It's the same thing as == 0, and I tried it too, but same problem. I've been going through my code for the last 3 hours trying to catch something else. but with no luck.

    I didn't write the destructor, but I think I will to make sure I know what's going on.
    Everything is relative...

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you have exceptions set to stop always in the debugger? Break into the debugger and look at the call stack when the assertion happens. Look at the memory of the "this" variable at the different levels of the call stack. Is any of the data weird (like 0xfeeefeee or 0xcdcdcdcd)?

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    >>>Do you have exceptions set to stop always in the debugger?
    I do.
    but it's not an assetion error as I think I said before. It's a debug error. Says "Please try to debug the appliaction".

    I'm checking if I freed/deleted all the arrays and such.
    Everything is relative...

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    ok, I tried to figure this out before but with no luck. Now i get the same demage in block 1180 error if I leave these lines in the code of a different function:
    Code:
    // allocation
          Units = (char **)calloc(NumParam, sizeof(char *));
    
       for(i=0; i<NumParam; ++i)
    	   Units[i] = (char *)calloc(NameLengthU,sizeof(char));
    
    // deletion
    
          for(i=0; i<NumParam; i++)
    		free(Units[i]);    
    
    	free(Units);
    If the red lines are there, I get the same error. Otherwise this function run OK, or at least it seems so.
    Everything is relative...

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
            for(i=0; i<NumParam; i++)
    		free(Units[i]);    
    
    	free(Units);
    Well, of course you're getting an illegal operation of some kind. You're freeing the memory twice!

    You shouldn't cast malloc(). And in C++ you shouldn't use malloc() (or calloc()); malloc() doesn't invoke an object's constructor, while new does.
    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.

  11. #11
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I know I'm mixing C and C++ and I shouldn't. It's a big job to change all the C into C++ in this project. And I thought since it was allocated for a 2D then I should free the memory individually for all the rows and then the column array. But I guess I don't have to. thanks
    Everything is relative...

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Can you switch to new and delete?
    How about:
    Code:
    // allocation
          Units = new char* [NumParam];
    
       for(i=0; i<NumParam; i++)
    	   Units[i] = new char[NameLengthU];
    
    // deletion
    
          for(i=0; i<NumParam; i++)
    		delete [] Units[i];    
    
    	delete [] Units;
    I don't know much about calloc and free, but it actually looked ok the way you were doing it (you do have to free each row)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> It's a debug error. Says "Please try to debug the appliaction".

    Make sure you are running the program through the debugger (with Debug->Start, Go or F5) with your IDE. I assume that you are using VC++ and that you are using a Debug build for this testing.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The way you are freeing is fine. A typical cause of this problem is that you are not leaving room for a nul terminator at the end of your strings. Does NameLengthU take account of the nul terminator?

  15. #15
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Now it does...and it's ok.

    The original vector is still problematic. It may be the same thing. Does sscanf() attach a null terminator is it reads into a string?
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM