Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I faced some strange things is that I used CArray as a container for some custom class object. as the nbegining the program works well, CArray can operate .

but I don't know when I run the program after a while (maybe half hours, during that time I was writing some other custom class ) CArray blow up. it did not report error, but it blows up when program is running. it can not return its size, can not add element, can not do anything.

codes as blow.
#include "Afxtempl.h"
#include "Feature.h"
#include "BatchProcess.h"

class CFeatureSet					
{
public:
	CVector GetVector(int index);
	int GetFeatureVectorCount();
	CString ShowVectore();
	void GenerateFeatureVector();
	void Add(BatchProcess  * m_batch);
	CTraitCollection* GetTraitByIndex(int i);
	int GetFeatureCount();
	
	CFeatureSet();
	virtual ~CFeatureSet();
	
private:
	void Add(CTraitCollection* m_trait);
	CTraitCollection * m_Ptrait;					//Á´±íÖд洢
	CTraitCollection * m_pLastTrait;	

	CArray<ctraitcollection ctraitcollection=""> m_Traitarry;	   };
</ctraitcollection>


//it blows up here when program is running(at both)
m_Traitarry.Add(m_trait);
return m_Traitarry.GetSize();


class definition is:
#include "Feature.h"
#include "Afxtempl.h"
class CTraitCollection  
{
public:
	CString GetWpString(int index);
	int CalcDelayStrokePos(int index);
	int GetDelayStrokeCountofWp(int index);
	void Draw(CDC* pDC);
	CTraitCollection();
	virtual ~CTraitCollection();

	CFeature * GetFeatureByindex(int index);
	int GetFeatureNum();
	CString ShowData();
	void LoadFeature(DeSerializer * _Deserializer);
	void Clear();
	void GetWordInfo(CString HandString, short m_line,CRect _WRect);
	CString m_handstring;				
	short  m_baseline;							
	CRect m_WordRect;										 
	short m_wpCount;                       CMap<int,int,cstring,cstring>;m_WpUnicode; 	
void AddFeature(CFeature * m_Feature);
	
	CFeature* m_pFeature;
	CFeature* m_pLastFeature;
	
	CTraitCollection * m_PnextTrait;
};



when I replaced CArray with CMap , there is still same problem. when program runs it blows up at any operation of CMap.
is there anyone experienced that same problem before ? or is there anyone who could tell me why and how fix that problem?
Posted
Updated 17-Sep-10 4:46am
v2

1 solution

When it 'Blows up' what is the exception?

<br />
// this might help find out the exception<br />
TRY <br />
{<br />
  Traitarry.Add(???);<br />
} <br />
CATCH(CException& e) <br />
{<br />
   TRACE("Exception %s\n", e.Message);<br />
}<br />

It could be CMemoryException for example.


Is m_Traitarry accessed by multiple threads, this could cause an exception if one thread is adding and another is reading or adding. If this is your problem you will need to protect it with a critical section or Mutex.

e.g.
<br />
CArray<???> m_Traitarry<br />
CCriticalSection m_TraitarryAccess<br />


<br />
void AddToArray(???)<br />
{<br />
   CSingleLock lock(&m_Traitarry, TRUE);<br />
   Traitarry.Add(???); <br />
}<br />
 
Share this answer
 
Comments
Alimjan Yasin 18-Sep-10 3:03am    
m_Traitarry is not accessed multiple threads. it can not add or other opearation when program is running.
ARopo 20-Sep-10 15:30pm    
What is the exception? try going to the debug menu an check break when c++ runtime exceptions are thrown.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900