Using CString into static method
I am stuck into a strange problem. I have MDI app, which has a struct into CDocument:
Code:
class CMyDoc : public CDocument
{
...
struct SRecord
{
SRecord(){}
virtual ~SRecord(){}
CString sName;
CString sState;
CString sDateu;
CString sDatec;
};
CTypedPtrArray<CPtrArray, SRecord*> m_arrRecord;
and somewhere in document I load this struct with data:
Code:
SRecord* pItem = new SRecord;
pItem->sName = saItem.GetAt(ML_ASSETNAME);
pItem->sState = saItem.GetAt(ML_STATE);
pItem->sDateu = saItem.GetAt(ML_DATEU;
pItem->sDatec = saItem.GetAt(ML_DATEC);
m_arrRecord.Add(pItem);
ok by now. I am trying to sort data:
Code:
void CMyDoc::SortData(int nColumn, BOOL bAscending)
{
switch(nColumn)
{
case 9:
if(bAscending)qsort((void*)m_arrRecord.GetData(), m_arrRecord.GetSize(), sizeof(SRecord), CompareDateUAscending);
else qsort((void*)m_arrRecord.GetData(), m_arrRecord.GetSize(), sizeof(SRecord), CompareDateUDescending);
break;
...
}
but the problem occur when data is access in static method:
Code:
//static method:
int CMyDoc::CompareDateUDescending(const void* arg1, const void* arg2)
{
SRecord* Record1 = *(SRecord**)arg1; // <-- OK
SRecord* Record2 = *(SRecord**)arg2; // <-- Unhandled exception* see note below
if(Record1->sDateu.IsEmpty() || Record2->sDateu.IsEmpty())
return 0;
COleDateTime dL, dR;
dL.ParseDateTime(Record1->sDateu);
dR.ParseDateTime(Record2->sDateu);
return (dL == dR ? 0 : (dL < dR ? 1 : -1));
}
*and the crash told me:
"An unhandled exception was encountered during a user callback."
What could be the problem ? Can you help me ? Thank you.
Re: Using CString into static method
What is the value of arg2 when it is attempted to be dereferenced - and is it valid? If you single step through this code with the debugger what happens?
What is the type of m_arrRecord.GetData() ?
Assuming .GetData() returns a pointer to an array of SRecord, then shouldn't the code be ??
Code:
SRecord* Record1 = (SRecord*)arg1;
SRecord* Record2 = (SRecord*)arg2;
PS in light of ovidiucucu's post #3 stating that the elements of m_arrRecord are pointers to SRecord and not objects of SRecord, then my above assumption re what .Getdata() returns isn't right. :blush:
Re: Using CString into static method
The elements of m_arrRecord are pointers and not objects of SRecord type.
So, you have to pass sizeof(SRecord*) and not sizeof(SRecord).
Re: Using CString into static method
"What is the type of m_arrRecord.GetData() ?"
void**
"then shouldn't the code be ??"
I have already tried that, doesn't worked, crash the program here:
Code:
SRecord* Record1 = (SRecord*)arg1;
and led me here:
Code:
CStringData* GetData() const throw()
{
return( reinterpret_cast< CStringData* >( m_pszData )-1 ); // the crash lead me on this line
}
Re: Using CString into static method
Quote:
Originally Posted by
ovidiucucu
The elements of m_arrRecord are pointers and not objects of SRecord type.
So, you have to pass sizeof(SRecord*) and not sizeof(SRecord).
Ovidiu, it works now !! Kindly thank you again !!!