Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ a method works only in main function

The idea is to make inscriptionons on top of any application

I have this code in my main.cpp

#include "OVERLAY.h"
void notWorking(Overlay &ol)
{
    ol.addLabel(100, 100, L"ASDASDASD", RGB(255, 0, 0));
}
int main()
{

    Overlay ol;

    ol.addLabel(100, 100, L"ASDASDASD", RGB(255, 0, 0));
    //notWorking(ol);
    
    for (;;)
    {
        ol.drawAll();
    }

}

When I do ol.addLabel(100, 100, L"ASDASDASD", RGB(255, 0, 0)); a label appears on the screen,
but when I call notWorking(ol); it appears for a split second and then gone.
Basically I can’t call addLabel() anywhere beside my main function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Overlay class:

class Overlay
{
private:
    std::vector<Label> labels;
    HDC hdc;
    
public:
    void addLabel(int x, int y, std::wstring title, COLORREF clr)
    {
        Label label;
        label.create(x, y, title, hdc, clr);
        label.draw(2); 
        labels.push_back(label);
    }
    
    void drawAll()
    {   
        for (int i =0; i < labels.size(); i++)
            labels[i].draw();   
    }


    Overlay()
    {   
        hdc = GetDC(GetDesktopWindow());    
    }
};

Label class:

class Label
{
    int x, y;
    std::wstring txt;
    HDC* wdc;
    COLORREF color;
public:
    Label()
    {

    }
    void create(int x, int y, std::wstring text, HDC wdc, COLORREF color)
    {
        this->x = x;
        this->y = y;
        this->txt = text;
        this->wdc = &wdc;
        this->color = color;
    }
    void draw(int state = 1)
    {
        RECT rect;

        SetTextColor(*wdc, color);
        
        SetBkMode(*wdc, state); 
        rect.left = x;
        rect.top = y;

        DrawText(*wdc, txt.c_str(), -1, &rect, DT_NOCLIP);
    }
};

I tried calling addLabel() within its class, but even then it didn’t work

>Solution :

In Label, you are storing an HDC* pointer to an HDC variable that is local to the Label constructor, and which goes out of scope when the constructor exits, thus leaving the pointer dangling. Any use of the HDC* pointer after that point is undefined behavior.

Which means, even the call to addLabel() inside of main() is subject to this problem. The fact that it works at all is pure luck.

HDC is already a pointer type to begin with, so you can safely copy it around and store it by its value (as Overlay does internally).

Change Label to remove the extra pointer indirection on the HDC, eg:

class Label
{
    ...
    HDC wdc;
    ...
public:
    ...
    void create(..., HDC wdc, ...)
    {
        ...
        this->wdc = wdc;
        ...
    }
    void draw(...)
    {
        ...

        SetTextColor(wdc, ...);
        
        SetBkMode(wdc, ...); 
        ...

        DrawText(wdc, ...);
    }
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading