Skip to content

[3.x] Initialize all variables in constructor to prevent undefined behavior #52674

Closed
@qarmin

Description

@qarmin

Issue description

3.x version of #43636

Due using unitialized variables, Godot may behave very strange and cause bugs like #52634.
To prevent such situations, all variables(except maybe buffers) should be initialized in constructor, because it is easy to forget to do it later.

So, initializing all variables in such class

class HashingContext : public Reference {
	void *ctx;
        int rar;
        bool q;
public:
	HashingContext() {
            ctx = memnew(something);
        }
	~HashingContext();
};

can be done in this way in 3.x branch

class HashingContext : public Reference {
	void *ctx;
        int rar;
        bool q;
public:
	HashingContext() {
            ctx = memnew(something);
            rar = 0;
            q = false;
        }
	~HashingContext();
};

Do not try to create PR which changes more than ~50-100 lines, because it will be hard to rebase after other users changes and also such small PR should be merged faster into 3.x branch.

Maybe cppcheck html report will be helpful to find such variables - report.zip

Screenshot from 2021-09-14 17-39-42

Remember to put here info that you work on e.g. core/ or servers/ folder

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions