STL Iterating through a map causes access violation 
Author Message
 STL Iterating through a map causes access violation

I have the following declared in a header file:
**************************************************
class CodeTable
{
public:
 NCICCODES_API CodeTable(void);
 NCICCODES_API CodeTable(const CodeTable &rhs);
 NCICCODES_API CodeTable(const std::string, UINT);

 NCICCODES_API CodeTable &operator =(const CodeTable &rhs);

public:
 std::string  m_Code;
 std::string  m_Desc;
 UINT   m_TableType; // 1=One, 2=Two

private:
 bool operator <(const CodeTable &) const;
 bool operator==(const CodeTable &) const;

Quote:
};

/*--------------------------------------------------------------*
 *  CodeBook            *
 *--------------------------------------------------------------*/
typedef std::map<std::string, class CodeTable>   CodeBook;
typedef std::pair<const std::string, class CodeTable> CodeBookEntry;
************************************************

The following code is used to insert entries into the 'CodeBook'.
************************************************
  // -- Find CodeTable entry
  CodeBook::iterator i = gCodeBook.find(mfcCode);
  if (i == gCodeBook.end())
   {
   gCodeBook.insert(CodeBookEntry(mfcCode, CodeTable(mfcCode, 1)));  //
insert new
   i = gCodeBook.find(mfcCode);        // << find new
   assert(i != gCodeBook.end());        // Verify not at end
   CodeBookEntry cbe = *i;                // << visual inspection during
debug
   }
******************************************

The inserts seem to be working ok.  But when I try to access the map, I get
an access violation.

Here is the code:
***********************************
void ExportCodeBook(CComboBox &cb)
{
 CodeBook *pCodeBook = GetCodeBook();
 if (pCodeBook)
  {
  CodeBook::iterator ix = pCodeBook->begin();
  while(ix != pCodeBook->end())
   {
   CodeBookEntry cbe = *ix;    << -- will fail the second time around, not
first
   cb.AddString( ix->first.c_str() );
   ++ix;
   }
  }

Quote:
}

*************************************

I have to assume the '++ix' is not working correctly.  I know there are
three items in the map.
Executing the first time, the failing statement works (using iterater
begin()), but after incrementing the iterator, the statement fails.

It's driving me crazy.  Can anyone see anything unusual with what I have
done?

Thanks in advance.

Hank Williams
Quantum Technologies, Inc.



Fri, 31 Dec 2004 08:26:54 GMT  
 STL Iterating through a map causes access violation


Quote:
> I have the following declared in a header file:
> **************************************************
> class CodeTable
> {
> public:
>  NCICCODES_API CodeTable(void);
>  NCICCODES_API CodeTable(const CodeTable &rhs);
>  NCICCODES_API CodeTable(const std::string, UINT);

>  NCICCODES_API CodeTable &operator =(const CodeTable &rhs);

> public:
>  std::string  m_Code;
>  std::string  m_Desc;
>  UINT   m_TableType; // 1=One, 2=Two

> private:
>  bool operator <(const CodeTable &) const;
>  bool operator==(const CodeTable &) const;
> };

You don't show the code for these operators.
Wild guess: does the '<' operator return false when the two operands are
equal?

Luc



Fri, 31 Dec 2004 13:53:13 GMT  
 STL Iterating through a map causes access violation
Are you sharing your class accross DLLs? Because if this is the case then
you have a problem and you must obtain a new version of xtree from
http://www.dinkumware.com/vc_fixes.html

I had the same problem (first access worked, but after the first iteration
it failed) on Friday, but Craig Powers gave me the solution


Quote:
> I have the following declared in a header file:
> **************************************************
> class CodeTable
> {
> public:
>  NCICCODES_API CodeTable(void);
>  NCICCODES_API CodeTable(const CodeTable &rhs);
>  NCICCODES_API CodeTable(const std::string, UINT);

>  NCICCODES_API CodeTable &operator =(const CodeTable &rhs);

> public:
>  std::string  m_Code;
>  std::string  m_Desc;
>  UINT   m_TableType; // 1=One, 2=Two

> private:
>  bool operator <(const CodeTable &) const;
>  bool operator==(const CodeTable &) const;
> };

> /*--------------------------------------------------------------*
>  *  CodeBook            *
>  *--------------------------------------------------------------*/
> typedef std::map<std::string, class CodeTable>   CodeBook;
> typedef std::pair<const std::string, class CodeTable> CodeBookEntry;
> ************************************************

> The following code is used to insert entries into the 'CodeBook'.
> ************************************************
>   // -- Find CodeTable entry
>   CodeBook::iterator i = gCodeBook.find(mfcCode);
>   if (i == gCodeBook.end())
>    {
>    gCodeBook.insert(CodeBookEntry(mfcCode, CodeTable(mfcCode, 1)));  //
> insert new
>    i = gCodeBook.find(mfcCode);        // << find new
>    assert(i != gCodeBook.end());        // Verify not at end
>    CodeBookEntry cbe = *i;                // << visual inspection during
> debug
>    }
> ******************************************

> The inserts seem to be working ok.  But when I try to access the map, I
get
> an access violation.

> Here is the code:
> ***********************************
> void ExportCodeBook(CComboBox &cb)
> {
>  CodeBook *pCodeBook = GetCodeBook();
>  if (pCodeBook)
>   {
>   CodeBook::iterator ix = pCodeBook->begin();
>   while(ix != pCodeBook->end())
>    {
>    CodeBookEntry cbe = *ix;    << -- will fail the second time around, not
> first
>    cb.AddString( ix->first.c_str() );
>    ++ix;
>    }
>   }
> }
> *************************************

> I have to assume the '++ix' is not working correctly.  I know there are
> three items in the map.
> Executing the first time, the failing statement works (using iterater
> begin()), but after incrementing the iterator, the statement fails.

> It's driving me crazy.  Can anyone see anything unusual with what I have
> done?

> Thanks in advance.

> Hank Williams
> Quantum Technologies, Inc.



Fri, 31 Dec 2004 18:18:00 GMT  
 STL Iterating through a map causes access violation
The 'operator <' is not coded.  I have it as private so I can tell if it is
used.

--
Hank Williams
Quantum Technologies, Inc.


|


| > I have the following declared in a header file:
| > **************************************************
| > class CodeTable
| > {
| > public:
| >  NCICCODES_API CodeTable(void);
| >  NCICCODES_API CodeTable(const CodeTable &rhs);
| >  NCICCODES_API CodeTable(const std::string, UINT);
| >
| >  NCICCODES_API CodeTable &operator =(const CodeTable &rhs);
| >
| > public:
| >  std::string  m_Code;
| >  std::string  m_Desc;
| >  UINT   m_TableType; // 1=One, 2=Two
| >
| > private:
| >  bool operator <(const CodeTable &) const;
| >  bool operator==(const CodeTable &) const;
| > };
|
| You don't show the code for these operators.
| Wild guess: does the '<' operator return false when the two operands are
| equal?
|
| Luc
|
|
|



Fri, 31 Dec 2004 21:46:15 GMT  
 STL Iterating through a map causes access violation
The DLL will be used by three applications.  Currently testing with one
application.  I did get a copy of the 'xtree' header, but the results were
the same.

I changed the 'map' to 'list' and I was able to iterate through the list OK,
but now I have memory leaks which U am chasing down right now.

--
Hank Williams
Quantum Technologies, Inc.


| Are you sharing your class accross DLLs? Because if this is the case then
| you have a problem and you must obtain a new version of xtree from
| http://www.dinkumware.com/vc_fixes.html
|
| I had the same problem (first access worked, but after the first iteration
| it failed) on Friday, but Craig Powers gave me the solution
|
|
|


| > I have the following declared in a header file:
| > **************************************************
| > class CodeTable
| > {
| > public:
| >  NCICCODES_API CodeTable(void);
| >  NCICCODES_API CodeTable(const CodeTable &rhs);
| >  NCICCODES_API CodeTable(const std::string, UINT);
| >
| >  NCICCODES_API CodeTable &operator =(const CodeTable &rhs);
| >
| > public:
| >  std::string  m_Code;
| >  std::string  m_Desc;
| >  UINT   m_TableType; // 1=One, 2=Two
| >
| > private:
| >  bool operator <(const CodeTable &) const;
| >  bool operator==(const CodeTable &) const;
| > };
| >
| > /*--------------------------------------------------------------*
| >  *  CodeBook            *
| >  *--------------------------------------------------------------*/
| > typedef std::map<std::string, class CodeTable>   CodeBook;
| > typedef std::pair<const std::string, class CodeTable> CodeBookEntry;
| > ************************************************
| >
| > The following code is used to insert entries into the 'CodeBook'.
| > ************************************************
| >   // -- Find CodeTable entry
| >   CodeBook::iterator i = gCodeBook.find(mfcCode);
| >   if (i == gCodeBook.end())
| >    {
| >    gCodeBook.insert(CodeBookEntry(mfcCode, CodeTable(mfcCode, 1)));  //
| > insert new
| >    i = gCodeBook.find(mfcCode);        // << find new
| >    assert(i != gCodeBook.end());        // Verify not at end
| >    CodeBookEntry cbe = *i;                // << visual inspection during
| > debug
| >    }
| > ******************************************
| >
| > The inserts seem to be working ok.  But when I try to access the map, I
| get
| > an access violation.
| >
| > Here is the code:
| > ***********************************
| > void ExportCodeBook(CComboBox &cb)
| > {
| >  CodeBook *pCodeBook = GetCodeBook();
| >  if (pCodeBook)
| >   {
| >   CodeBook::iterator ix = pCodeBook->begin();
| >   while(ix != pCodeBook->end())
| >    {
| >    CodeBookEntry cbe = *ix;    << -- will fail the second time around,
not
| > first
| >    cb.AddString( ix->first.c_str() );
| >    ++ix;
| >    }
| >   }
| > }
| > *************************************
| >
| > I have to assume the '++ix' is not working correctly.  I know there are
| > three items in the map.
| > Executing the first time, the failing statement works (using iterater
| > begin()), but after incrementing the iterator, the statement fails.
| >
| > It's driving me crazy.  Can anyone see anything unusual with what I have
| > done?
| >
| > Thanks in advance.
| >
| > Hank Williams
| > Quantum Technologies, Inc.
| >
| >
| >
| >
| >
|
|



Fri, 31 Dec 2004 21:49:56 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Access Violation in call to malloc - causes?

2. pInvoke-Dispatch causes 0xC0000005 Access Violation - WHY?!?!

3. CDatabase causes Access Violation

4. Stucture cause access violation..

5. Call to AfxCopyValueByRef() in DBRFX.CPP changes value, causes access violation

6. Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation

7. CRowset::MoveNext cause access violations

8. SQLBindParameter cause access violation

9. SQLExecute() causes an access violation

10. CTime Member of Recordset... Causes access violation

11. CRowset::MoveNext cause access violations

 

 
Powered by phpBB® Forum Software