How to check if obj is deleted

Description

I would like to know if somebody has a good way of checking whether or not the object is deleted

What MCU/Processor/Board and compiler are you using?

I’m using the simulator

What LVGL version are you using?

A pretty new one, but I don’t think anything has changed

What do you want to achieve?

I would like to know if there is an easy way to check if obj is deleted

What have you tried so far?

I can’t really think of a good general way of doing it.

It’s impossible because C makes it impossible. Object references are just pointers to memory used to hold the object.

Suppose you were to create an object, delete it, and then make another object. The C allocator would notice the free memory and create the new object in that location. Thus, the same reference would be used for the new object.

The C approach is to keep track of when you delete the object and set all pointers referencing it to NULL. There is no better way of doing this in C that I know of - it’s a limitation of the language.

So I would have to do something like this:

//inside function or callback
if (obj == ObjToCheckFor) {
      ObjToCheckFor = NULL
}// and so on...


//and this outside the function when i want to check if its deleted or not, and run code depending on it

if (ObjToCheckFor != NULL) {
     //run whatever here
}

I think that would work.

For those finding this via search, this is an alternative way depending on your intent:

if (lv_obj_is_valid(ui_obj))  {
     //run whatever here
}
1 Like