WeakRef question

Hi Folks,

I’ve not had call to use this before and I want to make sure I understand it before I spend a bunch of time today rewriting something :wink:

I need to be able to insert some extra headers into a webFile being sent from a web app. Unfortunately it doesn’t appear you can do this with the webFile and webPicture objects as they are currently, correct me if I’m wrong about that though! (feature request link here: <https://xojo.com/issue/42941> ) so I’m going to work up my own version of the same thing where I can send headers from a handleURL call.

I want to do something similar where the link to the file is valid only while the object is valid in memory. Normally I would create a uniqueID for the object and put that into a global dictionary. The handleURL method would pull that ID out of the path, look up the object and send it’s data. But this creates another reference to the object in the dictionary and the object will never be removed.

What I think I want to do is create a weakRef object from the class and put that into the dictionary.

That won’t count as another reference to the object, so when it goes out of scope in my code the destructor of the class will run normally and I can remove the weakRef from the dictionary in that event. If the weakref returns nil or isn’t in the dictionary I can return a not found page.

This way I don’t have to add another call to close or something that does that work all through the code that uses these, I can use the destructor to remove the class from the global index.

Do I understand that properly? The weakRef will let me get the object as long as it’s still referenced somewhere, but doesn’t count as a reference itself so the object will go out of scope normally?

That is how it works, and it is precious in a web app. When the WeakRef turns nil, that means the reference it pointed to is gone.

Thank you.

While the weakRefs are working great, there is something radically different about the handling of the data, it’s MUCH slower doing myself even though I’m just doing a lookup for the dictionary by ID and printing the memoryBlock of picture data stored there. Can’t use this, I’ll have to figure that out, but must roll these back for now sigh…

I haven’t noticed any dramatic speed issues. Can you post your code?

It was pretty dramatic, 5x fewer displays in the same time! The problem was with the browser trying to re-use the connection. I added a “connection: close” header to my custom webFile implementation and that seems to have solved the problem.

now I need to test if being able to add these headers will actually solve the problem I embarked on this little adventure to solve in the first place :wink: