CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    399

    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.
    Last edited by mesajflaviu; February 13th, 2017 at 06:58 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    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.
    Last edited by 2kaud; February 13th, 2017 at 08:30 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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).
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Jan 2009
    Posts
    399

    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 
    }

  5. #5
    Join Date
    Jan 2009
    Posts
    399

    Re: Using CString into static method

    Quote Originally Posted by ovidiucucu View Post
    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 !!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured