Thread: VirtualDub2
View Single Post
Old 23rd May 2017, 11:25   #271  |  Link
shekh
Registered User
 
Join Date: Mar 2015
Posts: 775
I say DeInit has no value because it does not do anything differently.
You can see in your code:

Code:
void __cdecl VDXVideoFilter::FilterDeinit   (VDXFilterActivation *fa, const VDXFilterFunctions *ff) {
	VDXVideoFilter *pThis = *reinterpret_cast<VDXVideoFilter **>(fa->filter_data);

	pThis->fa=fa;
	pThis->DeInit();
	pThis->~VDXVideoFilter();
}
DeInit and destructor are called in a row. There is no other entry point to either one! So you can just move code from DeInit to destructor and it will be the same.

Quote:
you musn't create your own constructor/destructor in your filter
Wrong, you must create them!

I understand the idea of Init-Deinit symmetry, but it is impossible. The instance can be created differently, but is must always destroy itself the same way, no matter how it was created. There is no way you can run a complete destructor for "initial" object and partial destructor for cloned object. This cannot work.

I remember the copy-constructor issue.

The proper filter example:

Code:
class Filter: public VDXVideoFilter {
public:
  Param param;
  void* buf;

  Filter(){ init(); }
  Filter(const Filter& a){ init(); param=a.param; }
  void init(){ buf=0; }
  ~Filter(){ free(buf); }
};
Here I have:
default constructor "Filter(){ init(); }"
copy constructor "Filter(const Filter& a){ init(); param=a.param; }"
helper function to avoid typing the same shit in both constructors "void init(){ buf=0; }"
destructor "~Filter(){ free(buf); }"

This is bare generic minimum to implement/think of.

What If instead I write something like this:

Code:
class Filter: public VDXVideoFilter {
public:
  Param param;
  void* buf;

  virtual Init(){ buf=0; }
  virtual DeInit(){ free(buf); }
};
Because I do not declare copy constructor it defaults to something like this:
Code:
  Filter(const Filter& a){ param=a.param; buf=a.buf; }
So when the clone object is created it will share the same memory block pointed by buf, eventually this will result in double-free memory corruption.
How do you plan to sort this with virtual DeInit? It does not help at all. Only copy constructor can decide what to do.
__________________
VirtualDub2
shekh is offline   Reply With Quote