MZ Custom DestinationBitmap reverts to square upon scene change

I tried to have a crack at replacing the square, that pops up when you click or tap on the map, with a custom bmp. It works, but it seems to revert back to square when you either open the menu or go into battle. Upon map transfer, the custom bmp is used again.

JavaScript:
// ================================================== //
// ChangeDestinationBitmap.js
// ================================================== //


/*:
 * @target MZ
 * @plugindesc Changes the BORING square when you click/tap a destination
 * @author Werzaque
 * @url https://
 * @help Free to use and/or modify for any project, no credit required.
 *
 * @param DestBMP
 * @text Destination Bitmap
 * @desc The file name of the picture replacing the default square
 * @type file
 * @dir img/pictures
 *
 * @param FrameCount
 * @desc How fast you want the animation to be
 * @min 5
 * @default 20
 * @type number
 */
 
void (() => {
'use strict';


    const PLUGIN_NAME = "ChangeDestinationBitmap";
    const parameters = PluginManager.parameters(PLUGIN_NAME);
    var pDestBMP = parameters["DestBMP"];
    const pFrameCount = Number(parameters["FrameCount"]);

void (alias => {
    Sprite_Destination.prototype.createBitmap = function() {
        alias.apply(this, arguments);
        if(pDestBMP) {
            this.bitmap = ImageManager.loadPicture(pDestBMP); //line to be overwritten in original function
        }
    };
})(Sprite_Destination.prototype.createBitmap);


Sprite_Destination.prototype.updateAnimation = function() {
    this._frameCount++;
    this._frameCount %= pFrameCount;
    this.opacity = (pFrameCount - this._frameCount) * 6;
    this.scale.x = 1 + this._frameCount / pFrameCount;
    this.scale.y = this.scale.x;
};


})();

After consulting with Master @ATT_Turan (nyan), he suggested that maybe an overwrite would be better, so I also tried it with the below. The result was exactly the same.

JavaScript:
Sprite_Destination.prototype.createBitmap = function() {
    const tileWidth = $gameMap.tileWidth();
    const tileHeight = $gameMap.tileHeight();
    this.bitmap = new Bitmap(tileWidth, tileHeight);
    if(pDestBMP)
        this.bitmap = ImageManager.loadPicture(pDestBMP);
    else
        this.bitmap.fillAll("white");
    this.anchor.x = 0.5;
    this.anchor.y = 0.5;
    this.blendMode = 1;
};

Next, to investigate why it reverts to square, the below was added to the code. The result was now that the bitmap would not revert to square after opening and closing the menu. But would still revert to square after battle. It would again change to the bitmap upon map transfer.

JavaScript:
Sprite_Destination.prototype.destroy = function(options) {
    if (this.bitmap && SceneManager._scene instanceof Scene_Battle)
        this.bitmap.destroy();
    Sprite.prototype.destroy.call(this, options);
};

Does anyone have any idea why it reverts to square again (i.e. why the if(pDestBMP) {this.bitmap = ImageManager.loadPicture(pDestBMP);} seems to not happen) when changing scenes? The reverting upon scene change can be prevented entirely with the below (which is essentially a long-winded way of saying "never destroy"), but I am puzzled why it would do this in the first place...

JavaScript:
Sprite_Destination.prototype.destroy = function(options) {
    if (this.bitmap && !(SceneManager._scene instanceof Scene_Battle || SceneManager._scene instanceof Scene_Menu))
        this.bitmap.destroy();
    Sprite.prototype.destroy.call(this, options);
};
The reverting upon scene change can be prevented entirely with the below (which is essentially a long-winded way of saying "never destroy"), but I am puzzled why it would do this in the first place...

Why not the short-winded way?
Code:
Sprite_Destination.prototype.destroy = function(options)
{
    Sprite.prototype.destroy.call(this, options);
};
Why not the short-winded way?
The thinking was that I don't know if there is a situation in which destroy needs to happen on scene_map.

EDIT: for that matter I'm not even sure if suppressing it this way will not have any negative effects somewhere.
As you've discovered, you shouldn't destroy the bitmap. Destroy is for when you've created a temporary texture that you will redraw again when needed, like the usual white square of Sprite_Destination. It's not for something that you've loaded from file and want to persist.

RMMZ caches images loaded from file. The image cache is cleared on map transfer, but not on other scene changes. The bitmap is a reference to the cache, so destroying it also destroys the cached image.

I would suggest using loadSystem in this situation, since that'll put the image into persistent cache, meaning it won't have to reload the image every map transfer.

E.g. (untested):
Spoiler: plugin code
JavaScript:
// header
// parse params
;void (() => { if (!pDestBMP) return;  // don't patch if parameter is not set.

    // Patch - set bitmap to system image.
    void (alias => {
        Sprite_Destination.prototype.createBitmap = function() {
            alias.apply(this, arguments);
            this.bitmap = ImageManager.loadSystem(pDestBMP);
        };
    })(Sprite_Destination.prototype.createBitmap);

    // Override - revert to inherited method.
    delete Sprite_Destination.prototype.destroy;

})();
Thank you, this works! Although I am not sure if I understand the explanation, because with map transfers the custom image was displayed correctly. So why does it load the picture into pDestBMP again upon map transfer, but not when returning from menu or battle? I assume under the default code it is destroyed in all three cases, but only with map transfer is it "reloaded". At least that's how it seems to me.

EDIT: by the way, I changed it to ImageManger.loadPicture(pDestBMP). It does seem to work but I hope it is equally valid.
On map transfer, the cache is cleared. This is the difference.

Until the cache is cleared, the bitmap object is still present. The problem is that once destroyed, a bitmap should not be referenced: its texture will appear blank and its resources may already have been released from memory by the garbage-collector.

By default the bitmap is destroyed in all cases, because it's just a white square in all cases. It does not get stored in the engine's image cache: no need to cache something so simple when it can just be redrawn on demand.

Also, just to be clear: pDestBMP is just a URL/filename. It never stores a bitmap reference or the associated texture. 👋
On map transfer, the cache is cleared. This is the difference.
I could hear the many pennies drop in my head. Thank you!

Attaching the definitive version here.
This thread is being closed due to being solved. If you need it reopened, report it to let us know why. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/custom-destinationbitmap-reverts-to-square-upon-scene-change.167424/