Prev: about changing dialog box property at run time
Next: [Q] CTabCtrl/CBitmapButton: custom color for the WHOLE background
From: Adam on 10 Mar 2010 05:23 Hello, I have problems using CArray<> with hash_map<>. What's wrong with the following code ? class A { A() {} A( const A& ) {} ~A() {} void operator=( const A& ) {} stdext::hash_map<int,int> m_member; }; .... A item; CArray<A> test; test.Add( item ); test.Add( item ); test.RemoveAll(); // access violation in hash_map<> destructor ???????? The same code with std::map<> works ok. What is the problem ?. Everything tested under VS.2005. Thanks, Adam
From: Giovanni Dicanio on 10 Mar 2010 05:50 "Adam" <adam_mich(a)gazeta.pl> ha scritto nel messaggio news:80954482-e4f9-4b54-b24e-949c9d5df047(a)i25g2000yqm.googlegroups.com... > I have problems using CArray<> with hash_map<>. What's wrong with the > following code ? > > class A > { > A() {} > A( const A& ) {} > ~A() {} > void operator=( const A& ) {} > stdext::hash_map<int,int> m_member; > }; I believe this is not actual code. As you wrote that, all members are private. Giovanni
From: Giovanni Dicanio on 10 Mar 2010 05:58 "Adam" <adam_mich(a)gazeta.pl> ha scritto nel messaggio news:80954482-e4f9-4b54-b24e-949c9d5df047(a)i25g2000yqm.googlegroups.com... > I have problems using CArray<> with hash_map<>. What's wrong with the > following code ? > > class A > { > A() {} > A( const A& ) {} > ~A() {} > void operator=( const A& ) {} As already written, I susepct there is a missing "public:" in the above class listing. Moreover, the usual patterno for assignment operator is: A & operator=(const A &) Moreover, if the member-wise copy is OK, and if each data member is a RAII class that frees its own stuff, you should rely on C++ compiler default implementation of copy constructor , assignment operator, constructor and destructor. In other words, in your case, this should do OK: <code> class A { public: // // Compiler generated default ctor, copy ctor, operator= and dtor are OK. // stdext::hash_map<int,int> m_member; }; </code> Note that STL containers are very well composable together. If you use std::vector instead of MFC CArray, this code will work fine: <code> A item; std::vector<A> test; test.push_back(item); test.push_back(item); test.clear(); </code> HTH, Giovanni
From: Adam on 10 Mar 2010 06:57 Hi, Of course all members of A should be public. My mistake, sorry ! Of course compiler can generate default copy constructor itself in that case Of course I should use STL containers. It is old, legacy code which uses CArray<>. I've just added one member - stdext::hash_map and got those Access Violation errors. It is not my production code, it's just a code snippet to reproduce actual behaviour. Now I have even simpler one : typedef stdext::hash_map<int,int> MAP; I found that when I add one element everything is ok : CArray<MAP> test; MAP item; test.Add( item ); test.RemoveAll(); // this is ok ! But when I add two or more elements I get access violation CArray<MAP> test; MAP item; test.Add( item ); test.Add( item ); test.RemoveAll(); // unexpectable access violation ! Do you know what is the reason of those errors ? Thanks in advance, Kamil
From: Giovanni Dicanio on 10 Mar 2010 07:31
"Adam" <adam_mich(a)gazeta.pl> ha scritto nel messaggio news:95879f74-8a60-43af-9483-0bac824a61aa(a)t20g2000yqe.googlegroups.com... > typedef stdext::hash_map<int,int> MAP; [...] > But when I add two or more elements I get access violation > > CArray<MAP> test; > MAP item; > test.Add( item ); > test.Add( item ); > test.RemoveAll(); // unexpectable access violation ! > > Do you know what is the reason of those errors ? These could be error messages generated by the Iterator Debugging feature of Microsoft VC++'s STL implementation. In fact, if you disable iterator debugging, these errors disappears (I verified that with VC9 [VS2008 SP1]: http://msdn.microsoft.com/en-us/library/aa985939(VS.80).aspx You can add the following lines in your StdAfx.h, before including STL headers: // Disable iterator debugging in debug builds #if defined(_DEBUG) #define _HAS_ITERATOR_DEBUGGING 0 #endif Giovanni |