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.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 23rd May 2019, 21:13   #1  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 1,810
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
}
The constructor creates an array of PVideoFrame and then populates it with pointers to every frame of the clip. This seems to work, as it keeps those frames live in Avisynth's cache and makes working with them much faster (for my particular purposes).

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).
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 23rd May 2019, 21:51   #2  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,094
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 ???
StainlessS is online now   Reply With Quote
Old 23rd May 2019, 22:05   #3  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 1,810
Yup, tried that, still crashed.
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 23rd May 2019, 22:28   #4  |  Link
Wilbert
Moderator
 
Join Date: Nov 2001
Location: Netherlands
Posts: 6,335
If you set it to NULL there is nothing to delete. I guess 'delete cache[];' should work since it's an array (but it will not do anything in this case).
Wilbert is offline   Reply With Quote
Old 23rd May 2019, 23:38   #5  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 1,810
Quote:
Originally Posted by Wilbert View Post
If you set it to NULL there is nothing to delete. I guess 'delete cache[];' should work since it's an array (but it will not do anything in this case).
"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).
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 23rd May 2019, 23:41   #6  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,094
Oops, yes, I did not notice that in your source, I should have.

Code:
cache = new PVideoFrame[end - start];
You have to use below syntax to delete, when allocating array as above, eg [blue part]

Code:
delete [] cache;
The [] bit causes it to call destructor on each element of the array.
__________________
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.
StainlessS is online now   Reply With Quote
Old 24th May 2019, 07:45   #7  |  Link
pinterf
Registered User
 
Join Date: Jan 2014
Posts: 1,240
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
pinterf is offline   Reply With Quote
Old 24th May 2019, 21:52   #8  |  Link
wonkey_monkey
Formerly davidh*****
 
wonkey_monkey's Avatar
 
Join Date: Jan 2004
Posts: 1,810
Quote:
Originally Posted by StainlessS View Post
Code:
delete [] cache;
The [] bit causes it to call destructor on each element of the array.
That was the answer! Thanks. Not that it needs to cause destruction of each element, but it seems that syntax is required if new was used to create an array with [].
__________________
My AviSynth filters / I'm the Doctor
wonkey_monkey is offline   Reply With Quote
Old 25th May 2019, 00:56   #9  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 7,094
Quote:
Not that it needs to cause destruction
Not sure bout this but, perhaps when PVideoFrame set NULL, the secret pointer to data buffer is nullified [and reference count to the buffer are decremented],
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.
StainlessS is online now   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 20:43.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.