Iterator destructor problem

V

vivekian

Hi,

I have this following class

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};

childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}

childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}


This is a snippet of code which uses this class :

struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

When 'c' goes out of scope , i get a segmentation fault. There is no
explicit destructor declared since no dynamic memory was allocated.
But that is the point the seg fault occurs. What can be done to
prevent this error.

Thanks in advance,
vivekian
 
O

Obnoxious User

Hi,

I have this following class [snip]


This is a snippet of code which uses this class :

struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.
 
D

David Harmon

On Tue, 19 Jun 2007 12:34:31 -0000 in comp.lang.c++, vivekian
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

When 'c' goes out of scope , i get a segmentation fault. There is no
explicit destructor declared since no dynamic memory was allocated.
But that is the point the seg fault occurs. What can be done to
prevent this error.

I would be stepping through the code in some debugger, looking to see
what is actually happening there. If childInfo contains only an int and
an iterator then there should actually be no code executed to destroy
one. Perhaps there are other things happening at the end of that scope
than just the end of c.

If memory is being deallocated (perhaps for some reason unrelated to c)
that can often be the point where damage to the heap, that may have been
caused by some code elsewhere, becomes apparent.
 
B

Bharath

I have this following class [snip]

This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

It's always better to post a compilable code to help folks give you a
quick reply.
 
V

vivekian

I have this following class [snip]

This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}

childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

nodeInfo & nodeInfo::eek:perator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -> first , iNode -> second.meshId ) ;
iNode -> second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
 
V

vivekian

Hi,
I have this following class [snip]
This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;

};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;

};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);

} ;

NodeManager::NodeManager()
{

}

NodeManager::~NodeManager()
{

}

childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;

}

childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;

}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;

}

nodeInfo & nodeInfo::eek:perator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;

}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iteratoriNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -> first , iNode -> second.meshId ) ;
iNode -> second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iteratorpNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}

}

Can someone help me out on this one ?
Thanks in Advance,
Vivekian
 
J

John Harrison

Some corrections below

vivekian wrote:
vivekian said:
Hi,
I have this following class [snip]

This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}

childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

nodeInfo & nodeInfo::eek:perator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -> first , iNode -> second.meshId ) ;
iNode -> second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
Hi,
I have this following class [snip]

This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}

childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

nodeInfo & nodeInfo::eek:perator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -> first , iNode -> second.meshId ) ;
iNode -> second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

Your code has bugs.

First error is you write assignment operators and copy constructors when
they are not needed. This is a mistake.

More seriously this copy ctor is bugged

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

because you fail to allocate any space for the children vector.
Ironaically if you hadn't bothered to write this copy constructor, then
there would not have been a bug. Which is my first point, don't write
copy constructors or assignment operators when they are not needed, it's
asking for trouble. Assignment operator has simialar bug. As before, get
rid, problem goes away.

Third point, this line is bugged

c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;

perhaps you meant

c.relativeMeshId = atoi (meshId.substr(meshId.length()-1,1).c_str()) ;

john
 
V

vivekian

Some corrections below



vivekian wrote:
vivekian said:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[snip]
This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.
This is the code :
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};
class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};
class NodeManager
{
private:
std::map <const std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();
void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;


childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}
nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
nodeInfo & nodeInfo::eek:perator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}
void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -> first , iNode -> second.meshId ) ;
iNode -> second.meshId = meshId ;
}
std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[snip]
This is a snippet of code which uses this class :
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.
This is the code :
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};
class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};
class NodeManager
{
private:
std::map <const std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();
void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;


childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
childInfo & childInfo::eek:perator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}
nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
nodeInfo & nodeInfo::eek:perator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}
void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -> first , iNode -> second.meshId ) ;
iNode -> second.meshId = meshId ;
}
std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

Your code has bugs.

First error is you write assignment operators and copy constructors when
they are not needed. This is a mistake.

More seriously this copy ctor is bugged

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;

}

because you fail to allocate any space for the children vector.
Ironaically if you hadn't bothered to write this copy constructor, then
there would not have been a bug. Which is my first point, don't write
copy constructors or assignment operators when they are not needed, it's
asking for trouble. Assignment operator has simialar bug. As before, get
rid, problem goes away.

Third point, this line is bugged

c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;

perhaps you meant

c.relativeMeshId = atoi (meshId.substr(meshId.length()-1,1).c_str()) ;

john

Yes, thats what i thought initially, there should be no need of copy
constructor or assignment operators since there is no dynamic
allocation of memory. Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;
childInfo () {} ;
~childInfo () {} ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
~nodeInfo () {} ;
};

class NodeManager
{
private:
std::map <std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
void deleteRoute (const std::string childMacAddr,const std::string
meshId);
void getRoute (const std::string macAddr,const std::string
&meshId);
};

#endif /*NODEMANAGER_H_*/

//NodeManager.cpp
//---------------

#include "NodeManager.h"

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::vector<childInfo> trial ;
std::map <std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
iNode -> second.meshId = meshId ;
}

std::map <std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
childInfo c ;
c.relativeMeshId = atoi
(meshId.substr(meshId.length()-1,1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

// main.cpp

#include "LibMgr/NodeManager.h"

int main ( int argc , char *argv[] )
{
NodeManager n ;
n.addRoute("" , "a" ,"11") ;
n.addRoute("" , "b" ,"12") ;
n.addRoute("" , "c" ,"13") ;
n.addRoute("a", "d" ,"111") ;
n.addRoute("a", "e" ,"112") ;
n.addRoute("b", "f" ,"121") ;
n.addRoute("c", "g" ,"131") ;
n.addRoute("c", "h" ,"132") ;
n.addRoute("c", "i" ,"133") ;
}


// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault

Thanks,
Vivekian
 
J

John Harrison

// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault

Thanks,
Vivekian

I've tried your code with two different compilers VC++ 2005 and g++
3.4.4 (on cygwin) and your program runs without problems. I can't see
any bug either.

Strange.

john
 
V

vivekian

I've tried your code with two different compilers VC++ 2005 and g++
3.4.4 (on cygwin) and your program runs without problems. I can't see
any bug either.

Strange.

john

Its strange indeed. I am using g++ (GCC) 4.1.2 20061115 (prerelease)
(SUSE Linux).
Using gdb it seg faults at (*pNode).second.children.push_back ((c)) ;
The error is :

Program received signal SIGSEGV, Segmentation fault.
0xb7e7e4bf in std::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string ()
from /usr/lib/libstdc++.so.6

vivekian
 
V

vivekian

Its strange indeed. I am using g++ (GCC) 4.1.2 20061115 (prerelease)
(SUSE Linux).
Using gdb it seg faults at (*pNode).second.children.push_back ((c)) ;
The error is :

Program received signal SIGSEGV, Segmentation fault.
0xb7e7e4bf in std::basic_string<char, std::char_traits<char>,
std::allocator<char> >::basic_string ()
from /usr/lib/libstdc++.so.6

vivekian

Could it possibly be a bug in the STL implementation ? since it runs
fine in VC++.

Thanks,
vivekian
 
K

Kai-Uwe Bux

vivekian wrote:
[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

I wonder, how you got any output. I cannot get your code to compile.

// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>

You mean said:
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;

This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.

childInfo () {} ;
~childInfo () {} ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfo> children;
nodeInfo () {};
~nodeInfo () {} ;
};

class NodeManager
{
private:
std::map <std::string , struct nodeInfo > _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
void deleteRoute (const std::string childMacAddr,const std::string
meshId);
void getRoute (const std::string macAddr,const std::string
&meshId);
};

#endif /*NODEMANAGER_H_*/

//NodeManager.cpp
//---------------

#include "NodeManager.h"

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::vector<childInfo> trial ;
std::map <std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
iNode -> second.meshId = meshId ;
}

std::map <std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
childInfo c ;
c.relativeMeshId = atoi
(meshId.substr(meshId.length()-1,1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

// main.cpp

#include "LibMgr/NodeManager.h"

int main ( int argc , char *argv[] )
{
NodeManager n ;
n.addRoute("" , "a" ,"11") ;
n.addRoute("" , "b" ,"12") ;
n.addRoute("" , "c" ,"13") ;
n.addRoute("a", "d" ,"111") ;
n.addRoute("a", "e" ,"112") ;
n.addRoute("b", "f" ,"121") ;
n.addRoute("c", "g" ,"131") ;
n.addRoute("c", "h" ,"132") ;
n.addRoute("c", "i" ,"133") ;
}


// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault

Thanks,
Vivekian
 
V

vivekian

vivekian wrote:

[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>

You mean said:
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;

This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.

Even if i comment out iChild as a member , which would solves the
problem which you are facing, the program still segfaults. The code
without iChild is pasted at http://rafb.net/p/VOtPGW12.html . Line 83
seems to be the culprit. BTW , what error does the compiler throw ?

vivekian
 
K

Kai-Uwe Bux

vivekian said:
vivekian wrote:

[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>

You mean said:
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;

This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.

Even if i comment out iChild as a member , which would solves the
problem which you are facing, the program still segfaults. The code
without iChild is pasted at http://rafb.net/p/VOtPGW12.html. Line 83
seems to be the culprit.

Interesting. For me, it runs without problems.

What compiler are you using?

BTW , what error does the compiler throw ?

It complains about the type being incomplete:

vivekian_002.cc:15: instantiated from here
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++/4.1.1/bits/boost
_concept_check.h:216: error: '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
has incomplete type
vivekian_002.cc:10: error: forward declaration of 'struct nodeInfo'

I should remark that I built g++ with concept checks enabled.


Best

Kai-Uwe Bux
 
V

vivekian

vivekian said:
vivekian wrote:
[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :
I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
You mean <iostream>. There is no <iostream.h> in the C++ standard.
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;
This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.
Even if i comment out iChild as a member , which would solves the
problem which you are facing, the program still segfaults. The code
without iChild is pasted athttp://rafb.net/p/VOtPGW12.html. Line 83
seems to be the culprit.

Interesting. For me, it runs without problems.

What compiler are you using?
BTW , what error does the compiler throw ?

It complains about the type being incomplete:

vivekian_002.cc:15: instantiated from here
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++/4.1.1/bits/boost
_concept_check.h:216: error: '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
has incomplete type
vivekian_002.cc:10: error: forward declaration of 'struct nodeInfo'

I should remark that I built g++ with concept checks enabled.

Best

Kai-Uwe Bux

My bad. The problem was a namespace conflict. I was using struct
childInfo in another file in the project which had different fields.
When the vector children had to allocate memory it would pick up that
definition, hence seg faulting. Thanks for all the help and patience.

vivekian
 
R

Ron Natalie

vivekian said:
Could it possibly be a bug in the STL implementation ? since it runs
fine in VC++.

Thanks,
vivekian
\
No, John is right. Your program is incorrect.

One of the insidious forms of undefined behavior is
appearing to work normally on some days and not on others.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top