程序运行错误:CArray [英] Program running error : CArray

查看:61
本文介绍了程序运行错误:CArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些奇怪的事情,就是我使用CArray作为某些自定义类对象的容器.由于程序开始运行良好,因此CArray可以运行.

但是我不知道一段时间后(也许是半个小时,在这段时间里,我正在编写其他自定义类)运行程序时,CArray崩溃了.它没有报告错误,但是在程序运行时会炸毁.它无法返回其大小,无法添加元素,无法执行任何操作.

代码为打击.

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;
};




当我用CMap替换CArray时,仍然存在相同的问题.程序运行时,它在CMap的任何操作下都会炸毁.
有人遇到过同样的问题吗?还是有人可以告诉我为什么以及如何解决该问题?




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?

推荐答案

当它炸毁"时有什么例外?

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 />


例如,它可能是CMemoryException.


如果m_Traitarry被多个线程访问,则可能在一个线程正在添加而另一个线程正在读取或添加时导致异常.如果这是您的问题,则需要使用关键部分或Mutex保护它.

例如


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 />


这篇关于程序运行错误:CArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆