Does a UObject captured in lambda by value get retained?

Thanks for your reply!

Do you mean to wrap UObject and pass it to lambda, like this:

  TWeakObjectPtr<UMyObject> objPtr(myObject);
  processingThread.dispatch([objPtr](){
    if (objPtr->IsValid())
    {
      // do the processing
    }
  });

The problem is even if I do this check, the object might get GC’ed/deallocated while I’m inside of processing block.
If I was to avoid if check inside lambda and ensure validity of myObject during lambda execution, will using TStrongObjectPtr help?

In the meantime yesterday, I implemented a workardound using std::shared_ptr like this (let me know what you think of this):

// .h
struct FMyObjectStrongRefWrapper;

UCLASS(BlueprintType)
class MY_API UMyObject : public UObject
{
    GENERATED_BODY()
public:
    std::shared_ptr<FMyObjectStrongRefWrapper> getStrongRefObject();
}

UCLASS()
class UMyObjectStrongRef : public UObject
{
    GENERATED_BODY()
public:
    UPROPERTY()
    UMyObject * object_;
};

// .cpp
struct FMyObjectStrongRefWrapper
{
    FMyObjectStrongRefWrapper(UMyObject * obj)
    {
        check(IsValid(obj));

        objRef_= NewObject<UMyObjectStrongRef>();
        objRef_->object_= obj;
        objRef_->AddToRoot();
    }

    ~FMyObjectStrongRefWrapper()
    {
        check(IsValid(objRef_));
        objRef_->RemoveFromRoot();
    }

    UMyObjectStrongRef * objRef_;
};

std::shared_ptr<FMyObjectStrongRefWrapper> UMyObject ::getStrongRefObject()
{
    return make_shared<FMyObjectStrongRefWrapper>(this);
}

with this, I can create a FMyObjectStrongRefWrapper variable that I capture in lambda by-value like this (I might overload the -> operator for it too to avoid passing myObject):

  auto objRef = myObject->getStrongRefObject();
  processingThread.dispatch([objRef, myObject](){
      // do the processing on myObject...
  });