push_back(), assertion fails, MSVC

This is a discussion on push_back(), assertion fails, MSVC within the C++ Programming forums, part of the General Programming Boards category; I'm rather stumped. Using MSVC.NET, my code compiles fine, but when I run it, I get a "Debug Assertion Failed! ...

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    push_back(), assertion fails, MSVC

    I'm rather stumped. Using MSVC.NET, my code compiles fine, but when I run it, I get a "Debug Assertion Failed! .... File: dgbdel.cpp Line: 52 Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" dialog box when it tries to push_back(cSwitch). There's too much code to post everything, so I will attempt to post what I think is relevant.
    Code:
    class cPort
    {
    	private:
    		int connectionType;
    		int connectionIndex;
    	public:
    		BOOL connectedToSwitch();
    		BOOL setConnection(int index, int objectType);
    		int index();
    		cPort();
    };
    
    class cSwitch
    {
    	private:
    		cNetwork *network;
    		int indexOfThis;
    	protected:
    		void afunction();
    	public:
    		void anotherfunction();
    		cPort *port;
    		friend class cSwitch;
    		cSwitch(int index,cNetwork *networkPointer);
    		~cSwitch();
    };
    
    class cNetwork
    {
    	public:
    		vector<cSwitch> switches;
    		int AddSwitch();
    		friend class cSwitch;
    };
    I think the implementation details of the classes shouldn't matter for this. When running this code,
    Code:
    cNetwork networkA;  //works fine
    cSwitch temp(networkA.switches.size(),&networkA); //works fine
    networkA.switches.push_back(temp);
    It makes it past the two lines marked //works fine, but gives the error I listed above on the third line. Changing the code to
    Code:
    vector <cSwitch> networkAswitches;
    cSwitch temp(networkA.switches.size(),&networkA);
    networkAswitches.push_back(temp);
    had no effect; it still couldn't "push_back". When I break, I'm taken to the line
    Code:
      _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
    in dbgdel.cpp (in function void operator delete( void *pUserData)), and stepping out, it appears that it was called by
    Code:
    _CRTIMP int __cdecl _CrtIsValidHeapPointer(
            const void * pUserData
            )
    in dbgheap.c. Clearly, I did not write these two functions (and I can not step out any further). Have I done something wrong, or is this a strange bug in MSVC? Thank you.
    Away.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Try adding a copy constructor to your cSwitch class. I'm guessing something is double-deleting one of those pointers.

    [edit] Also, notice in this piece of code that you're using a pointer to a local variable. I hope you're not saving it in the private member network, since that variable will be out of scope as soon as the function is over.
    Code:
    cNetwork networkA;  //works fine
    cSwitch temp(networkA.switches.size(),&networkA); //works fine
    networkA.switches.push_back(temp);
    Last edited by pianorain; 06-14-2005 at 04:15 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Deo
    Deo is offline
    Registered User
    Join Date
    May 2005
    Posts
    73
    Hard to say... perhaps someone else can give you a more concrete answer, but I've gotten this error before and spent hours trying to fix it not knowing why it was occuring..

    For me my program was accidentally calling a destructor of a class when I didn't realize it and I tried to do something to the class that was just destroyed.

    I fixed it by running my debugger step by step and studying the values in my object... I noticed that suddenly the data went to corrupt bizzare crap when I didn't expect it and I realized uh oh.. it got destroyed prematurely.. (problem with a class containing pointers)

    May have nothing to do with your problem but its a thought.. I'm sure someone else with give u the correct answer.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Those functions in dbgdel.cpp are memory management functions, for allocation/deallocation of memory. You're getting that assertion error, but probably when running a release version of your program, that means a segmentation fault (or general protection fault in windows). Check if the pointers you keep are being well managed, and not deleted before any usage.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Those functions in dbgdel.cpp are memory management functions, for allocation/deallocation of memory. You're getting that assertion error, but probably when running a release version of your program, that means a segmentation fault (or general protection fault in windows). Check if the pointers you keep are being well managed, and not deleted before any usage.
    To expand a little: AFAIK, the error you're getting is coming from MSVC's automatic memory checking code that's inserted when compiling in debug mode. You'll come across a similar error whenever you write over memory that isn't yours; as has been mentioned, this can be caused by stuff like double deletion, array index out of bounds, etc.

    In fact, this is the reason that I try to avoid placing concrete objects in STL containers; too often, you'll end up with problems where a class requires a copy constructor performing a deep copy (kinda annoying), or worse, requires the addition of reference counting (very annoying, easy to bug up). As a result, I tend to store pointers instead of objects, or even better, reference-counting smart pointers (I use a smart-pointer class I wrote some time ago, otherwise Boost is probably a good option), and dynamically allocate the objects.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    confuted
    *gasp* Could it be?

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Quote Originally Posted by pianorain
    Try adding a copy constructor to your cSwitch class. I'm guessing something is double-deleting one of those pointers.

    [edit] Also, notice in this piece of code that you're using a pointer to a local variable. I hope you're not saving it in the private member network, since that variable will be out of scope as soon as the function is over.
    The copy constructor did the trick - thanks! I am indeed saving that pointer in network, but the cSwitch class is only used within the cNetwork class (normally ... I know I used it outside in the last little bit of code in my post), so when cNetwork goes out of scope, cSwitch is going away too, so it should be okay.

    Quote Originally Posted by sean_mackrory
    confuted
    *gasp* Could it be?
    It seems highly probable, doesn't it?
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Assertion error when looking for window handle
    By axr0284 in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2005, 04:11 PM
  3. MSVC resource script in Dev-C++
    By josh_d in forum Windows Programming
    Replies: 0
    Last Post: 03-17-2004, 04:07 PM
  4. GCC (cygwin) much faster than MSVC, Borland?
    By Sargnagel in forum C Programming
    Replies: 15
    Last Post: 08-05-2003, 04:15 AM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 07:02 PM

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