Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion. Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules. |
|
|
Thread Tools | Search this Thread | Display Modes |
|
23rd May 2019, 21:13 | #1 | Link |
Formerly davidh*****
Join Date: Jan 2004
Posts: 2,496
|
Trying to write a simple frame caching filter - need help with smart pointers, again
Here is a (simplified) snippet of code from a filter I'm writing to force frames to be cached by Avisynth:
Code:
FrameCache::FrameCache(PClip _child, IScriptEnvironment* env) : GenericVideoFilter(_child) { int i; cache = new PVideoFrame[end - start]; // cache is a private PVideoFrame* for (i = 0; i < vi.num_frames; i++) { cache[i] = child->GetFrame(i, env); } } FrameCache::~FrameCache() { int i; for (i = 0; i < vi.num_frames; i++) { cache[i - start] = NULL; } delete cache; // crashes here } But the filter crashes when the destructor is called. My understanding was that by setting the PVideoFrame elements to NULL, I would release the hold on those frames, but calling delete on cache causes a crash. Is this expected behaviour? I thought calling a delete on an array of NULL pointers should generally have no effect other than freeing up the memory for the array. If I use malloc/free instead of new/delete there's no crash, but I'd like to understand what's going on instead of just trusting that to work. No comments on what a terrible idea this might be, please It's for a very specific purpose and does its job perfectly (apart from the crash). |
23rd May 2019, 21:51 | #2 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
Have you just tried just calling delete on cache, without setting elements to NULL.
Delete on array of UDT's should call the desructor of each element prior to deleting the array itself. Dont know why its crashing though.
__________________
I sometimes post sober. StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace "Some infinities are bigger than other infinities", but how many of them are infinitely bigger ??? |
23rd May 2019, 23:38 | #5 | Link |
Formerly davidh*****
Join Date: Jan 2004
Posts: 2,496
|
"delete cache[];" is apparently not valid syntax. As I understand it, "delete cache;" should call delete on all the PVideoFrames in the array, but since they have been set to NULL, I would have expected it to do nothing. Something is trying to something, though. Maybe that kind of NULL safety isn't a feature of smart pointers, because simply setting them to NULL is equivalent to deletion (or so I understand).
|
23rd May 2019, 23:41 | #6 | Link |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
Oops, yes, I did not notice that in your source, I should have.
Code:
cache = new PVideoFrame[end - start]; Code:
delete [] cache;
__________________
I sometimes post sober. StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace "Some infinities are bigger than other infinities", but how many of them are infinitely bigger ??? Last edited by StainlessS; 23rd May 2019 at 23:47. |
24th May 2019, 07:45 | #8 | Link |
Registered User
Join Date: Jan 2014
Posts: 2,314
|
Anyway, you can insert forced caching of an intermediate clip into the filter creation process
See "InternalCache" here: http://avisynth.nl/index.php/Filter_SDK/Env_Invoke (I think cache hints are not relevant in avs+, dunno) Or actual usage https://github.com/pinterf/AviSynthP...anar.cpp#L3205 |
25th May 2019, 00:56 | #9 | Link | |
HeartlessS Usurer
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
|
Quote:
the 'Safe Pointer' object still exists and needs destruction, but would return (as if) null if interogated (weird secret C++ stuff).
__________________
I sometimes post sober. StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace "Some infinities are bigger than other infinities", but how many of them are infinitely bigger ??? Last edited by StainlessS; 25th May 2019 at 00:59. |
|
|
|