ucrtbased.dll: Access Violation

0

I am writing an extension (that is compiled as dynamic library), and I get extension error. Unfortunately, I have to draw entire GUI by myself, unfortunately, in pretty raw GDI/WinAPI. This is my code that crashes:


    Slider::Slider(const char* objectId, int height, POINT topLeft, int visibleElements, int totalElements, int elementHeight, EuroScopePlugIn::CRadarScreen* rs)
    {
        // Member variable initailization
        m_ObjectId = objectId;
        m_Height = height;
        m_TopLeft = topLeft;
        m_VisibleElements = visibleElements;
        m_ElementHeight = elementHeight;
        m_TotalElements = totalElements;
        m_Width = 10;
        m_pRS = rs;
        m_CurrentFirstElement = 0;
        m_CurrentLastElement = m_CurrentFirstElement + visibleElements;
    }

    SliderGrip::SliderGrip(Slider* sliderObject) : Slider(m_ObjectId.c_str(), m_Height, m_TopLeft, m_VisibleElements, m_TotalElements, m_ElementHeight, m_pRS)
    {
        // Base class member variable initialization
        {
            m_ObjectId = sliderObject->m_ObjectId;
            m_Height = sliderObject->m_Height;
            m_TopLeft = sliderObject->m_TopLeft;
            m_VisibleElements = sliderObject->m_VisibleElements;
            m_ElementHeight = sliderObject->m_ElementHeight;
            m_TotalElements = sliderObject->m_TotalElements;
            m_Width = sliderObject->m_Width;
            m_pRS = sliderObject->m_pRS;
            m_CurrentFirstElement = sliderObject->m_CurrentFirstElement;
            m_CurrentLastElement = sliderObject->m_CurrentLastElement;
        }
        m_GripHeight = (m_VisibleElements / m_TotalElements) * m_ElementHeight;
        m_GripTopLeft = (CPoint)(m_TopLeft.x, m_TopLeft.y + 10 + (m_CurrentFirstElement * m_ElementHeight));
    }

    void SliderGrip::RenderGrip(CDC* dc)
    {
        // localObjectId
        std::string localObjectId = "Grip";
        // Save DC for later
        int sDC = dc->SaveDC();

        // Create CRectangle
        CRect CRectangle(m_GripTopLeft.x, m_GripTopLeft.y, m_GripTopLeft.x + m_Width, m_GripTopLeft.y + m_GripHeight);

        // Grip Color
        COLORREF m_GripColor = RGB(0, 0, 0);

        // Fill CRectangle with CBrush
        CBrush RectangleFill(m_GripColor);
        dc->FillRect(&CRectangle, &RectangleFill);

        
        m_pRS->AddScreenObject(CELEMENTS::SCROLLBAR_GRIP, localObjectId.c_str(), CRectangle, 1, "");
        // Restore DC
        dc->RestoreDC(sDC);
        
        // Cleaning
        DeleteObject(RectangleFill);
    }

    SliderTrack::SliderTrack(Slider* sliderObject) : Slider(m_ObjectId.c_str(), m_Height, m_TopLeft, m_VisibleElements, m_TotalElements, m_ElementHeight, m_pRS)
    {
        // Base class member variable initialization
        {
            m_ObjectId = sliderObject->m_ObjectId;
            m_Height = sliderObject->m_Height;
            m_TopLeft = sliderObject->m_TopLeft;
            m_VisibleElements = sliderObject->m_VisibleElements;
            m_ElementHeight = sliderObject->m_ElementHeight;
            m_TotalElements = sliderObject->m_TotalElements;
            m_Width = sliderObject->m_Width;
            m_pRS = sliderObject->m_pRS;
            m_CurrentFirstElement = sliderObject->m_CurrentFirstElement;
            m_CurrentLastElement = sliderObject->m_CurrentLastElement;
        }

        m_TrackHeight = m_VisibleElements * m_ElementHeight;
        m_TrackTopLeft = (CPoint)(m_TopLeft.x, m_TopLeft.y + 10);
    }

    void SliderTrack::RenderTrack(CDC* dc)
    {
        // Save DC for later
        int sDC = dc->SaveDC();

        // CRectangle
        CRect CRectangle(m_TrackTopLeft.x, m_TrackTopLeft.y, m_TrackTopLeft.x + m_Width, m_TrackTopLeft.y + m_TrackHeight);

        // Color
        COLORREF m_TrackColor = RGB(200, 200, 200);

        // Fill CRectangle with CBrush
        CBrush RectangleFill(m_TrackColor);
        dc->FillRect(&CRectangle, &RectangleFill);

        // Restore DC
        dc->RestoreDC(sDC);

        // Cleaning
        DeleteObject(RectangleFill);
    }

    SliderButton::SliderButton() : Slider(m_ObjectId.c_str(), m_Height, m_TopLeft, m_VisibleElements, m_TotalElements, m_ElementHeight, m_pRS)
    {

    }

[...]

#pragma region Scrollbar
    // Scrollbar -> Elements::Slider --- grip, track, buttonup, buttondown
    Scrollbar::Scrollbar(const char* name, EuroScopePlugIn::CRadarScreen* rs, POINT topLeft, int height, int visibleElements, int totalElements, int elementHeight)
    {
        // member Var initialization 
        m_Name = name;
        m_pRS = rs;
        m_TopLeft = topLeft;
        m_Height = height;
        m_Width = 10;
        m_VisibleElements = visibleElements;
        m_TotalElements = totalElements;
        m_ElementHeight = elementHeight;

        // initialize objects
        sliderObject = new CInterface::Slider(m_Name.c_str(), m_Height, m_TopLeft, m_VisibleElements, m_TotalElements, m_ElementHeight, m_pRS);
        gripObject = new CInterface::SliderGrip(sliderObject);
        trackObject = new CInterface::SliderTrack(sliderObject);
    }

    void Scrollbar::AddTotalElements()
    {
        m_TotalElements++;
    }
    void Scrollbar::AddTotalElements(int count)
    {
        m_TotalElements += count;
    }
    void Scrollbar::RemoveTotalElements()
    {
        if(m_TotalElements>0)
            m_TotalElements--;
    }
    void Scrollbar::RemoveTotalElements(int Count)
    {
        if (m_TotalElements > Count)
            m_TotalElements -= Count;
    }

    void Scrollbar::Render(CDC* dc)
    {
        // for later
        int sDC = dc->SaveDC();
        
        gripObject->RenderGrip(dc);
        trackObject->RenderTrack(dc);

        // restore
        dc->RestoreDC(sDC);
    }

    Scrollbar::~Scrollbar()
    {
        delete sliderObject;
        delete gripObject;
        delete trackObject;
    }

#pragma endregion Scrollbar

Later on, code is called from main app in this way:

CallingFunction()
{
 testScroll = new CInterface::Scrollbar("testScroll", this, xyz, 50, 10, 50, 5);
}
~CallingFunction()
{
 delete testScroll;
}

Until now, code doesn't crash, but once I call rendering functions:

OnRender(HDC hDC)
{
 CDC    dc;
 dc.Attach(hDC);
 testScroll->Render(&dc);
 dc.Detach();
 dc.DeleteDC();
}

App crashes on startup with given code error:

Exception thrown at 0x78F6FF5C (ucrtbased.dll) in EuroScope.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD.

I have no clue what's causing this, any ideas? I am roughly sure it may be issue with some pointer and/or passed const char*. While I tried to replace const char* to std::string whenever possible, I got an similar error, but related to vcruntime140.dll...

c++
runtime
asked on Stack Overflow May 24, 2021 by Mateusz Żymła • edited May 24, 2021 by Mateusz Żymła

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0