Crash on editor close after creating texture

I’m trying to save a texture that I’ve created at runtime in the editor. I’ve finally had a chance to debug what I’m doing and I can see what’s going wrong.

First I create a package:


UPackage * Package = NewObject<UPackage>(nullptr, FName(*PackageName), RF_Standalone | RF_Public);

(What does a package do anyway?)

Then I create a transient texture so I can set the pixel format:


UTexture2D * temp = UTexture2D::CreateTransient(Image.mSize.x, Image.mSize.y, EPixelFormat::PF_R8G8B8A8);

Then, I construct a new non-transient object based on that:


UTexture2D * NewAsset = NewObject<UTexture2D>(Package, FName(*Filename), RF_Public | RF_Standalone, temp, false);

I force it to rebuild the platform data:


NewAsset->ForceRebuildPlatformData();

Then I check if the platform data exists:


if (NewAsset->PlatformData) {
    UE_LOG(LogTemp, Warning, TEXT("[Thing] It's cool we found platform data"));
}
else {
    UE_LOG(LogTemp, Error, TEXT("[Thing] NO PLATFORM DATA"));
}

Then I try to force resource creation:


FTextureResource * res = NewAsset->CreateResource();

At which point it tells me there’s no pixel format because platformdata is still null.

Is there some sort of post-create texture event I can hook into? What’s the right way to do this?

What should the package be?