Exception Error

This is a discussion on Exception Error within the C++ Programming forums, part of the General Programming Boards category; I am doing a tree search generation but I have an issue with the 2 functions below. The functions are ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    Exception Error

    I am doing a tree search generation but I have an issue with the 2 functions below.

    The functions are used to determine old nodes on the tree in order to stop further exploration.
    Function "isOld" is used to detect if there are already explored nodes with the same state information as the current node. It calls function "addInfo" to retrieve the list of tokens for already explored nodes.

    The issue here is: an exception error (first and second chance) occurs on line "return conjCodis;" that copies the node information to vector "codis".

    "First chance exception 0xC0000005 ACCESS_VIOLATION occurred at 0x00427D77, write of address 0x00000008 at 0x00427D77First chance exception 0xC0000005 ACCESS_VIOLATION occurred at 0x00427D77, write of address 0x00000008 at 0x00427D77
    and a Second chance exception on the same line."
    The error points to the "copy construct" of the vector.

    The code works pretty well for smaller trees but gives error on that line for longer time run (larger trees), that takes more than 12 hours since I have tree generation problems that can take more than 2 days to run.
    I used a 3rd party software to debug the code since it is not possible to run line debug for the time duration.

    Your help would be greatly appreciated. Ideas on what to do and a better way to do this are welcomed.

    Code:
    vector<double> Tree::isOld(vector<TokenTree*> *tok, int trans, double codi) const
    {
    	vector<double> interseccio;
    
    	vector<double> codis = conjPlaces.at(0)->addInfo(tok->at(0),codi); 
    	
    	int i=0;
    	interseccio=codis;
    	for(i=1; i< tok->size(); i++)
    	{
    		codis = conjPlaces.at(i)->addInfo(tok->at(i),codi);
    
    		int j = 0;
    		vector<double>::iterator posIter = interseccio.begin();
    		while(posIter!=interseccio.end())
    		{
    			vector<double>::iterator posicio = find(codis.begin(),codis.end(),*posIter);
    			
    			if(posicio==codis.end()){
    				posIter = interseccio.erase(posIter++);
    			}
    			else
    				++posIter;				
    		}
    	}
    
    	return interseccio;
    }
    Code:
    vector<double> Place::addInfo(TokenTree *marca, double codi)
    {
    	vector<double> conjCodis;
    
    	TreeToken tokAux = *marca;
    	map<TreeToken,vector<double> ,ltstr>::iterator pos = marcatges.find(tokAux); //marcatges is the data structure that stores the tokens for each node.
    	if(pos!=marcatges.end())
    	{
    		marca = const_cast<TreeToken*> (&((*pos).first));
    		conjCodis = marcatges[*marca];
    
    		marcatges[*marca].push_back(codi);
    	}
    	else
    	{
    		marcatges[*marca].push_back(codi);
    	}
    	return conjCodis;
    }

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Any ideas on the above?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You have a local vector being allocated when addinfo gets called, and you are returning it to the caller, but it goes away when the function ends. Don't do that. "New up" a vector, and return it instead.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Thanks for the suggestion(new the return vector). It worked on that part but led to another access violation on the vector that is being modified within the loop (interseccio) in the function. I remodified the function Tree::isOld a little.

    It returns the access violation error on line "vector<double>::iterator posIter = interseccio.begin();
    "
    (operation=,_UCopy, construct) after a long time run.

    I checked to see if the issue was an empty vector but that didn't help.

    Code:
    if(!interseccio.empty())
    	posIter = interseccio.begin();
    else
    	break;
    Thanks for your help as usual.

    Code:
    void Tree::isOld(vector<TokenTree*> *tok, int trans, double codi, vector<double> interseccio) const
    {
    	vector<double> codis;
    	interseccio = *(conjPlaces.at(0)->addInfo(tok->at(0),codi)); 
    	
    	int i=0;
    	
    	for(i=1; i< tok->size(); i++)
    	{
    		codis = *(conjPlaces.at(i)->addInfo(tok->at(i),codi));
    
    		vector<double>::iterator posIter = interseccio.begin();
    		while(posIter!=interseccio.end())
    		{
    			vector<double>::iterator posicio = find(codis.begin(),codis.end(),*posIter);
    			
    			if(posicio==codis.end()){
    				posIter = interseccio.erase(posIter);
    			}
    			else
    				++posIter;				
    		}
    	}
    
    }

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    xwb seems to agree with Dino here:
    Microsoft: Visual C++ - Exception Error
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Need to see more code.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    The difference between the first set of codes and the last one is just the return vector which i have done here (see below). Am I doing something wrong? Thanks for your help.

    Code:
    vector<double>* Place::addInfo(TokenTree *marca, double codi)
    {
    	vector<double>* conjCodis = new vector<double>();
    
    	TreeToken tokAux = *marca;
    	map<TreeToken,vector<double> ,ltstr>::iterator pos = marcatges.find(tokAux); //marcatges is the data structure that stores the tokens for each node.
    	if(pos!=marcatges.end())
    	{
    		marca = const_cast<TreeToken*> (&((*pos).first));
    		conjCodis = &marcatges[*marca];
    
    		marcatges[*marca].push_back(codi);
    	}
    	else
    	{
    		marcatges[*marca].push_back(codi);
    	}
    	return conjCodis;
    }

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    Can someone help me on this issue? I am running out of ideas...

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't really see how allocating a vector on the heap and returning it would make any difference, since you're returning a copy of it.

    The only thing I can think of right now is maybe you're running out of memory? You say it runs for a very long time, so maybe after a while, there's not enough contiguous memory left. Try catching a std::bad_alloc exception and see if that's what it is.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

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