Getting Segmentation fault while trying to call virtual destructor
<p>I'm getting a segmentation fault in my code while trying to calling destructor using a type-casted pointer . But If I change the destructor as a non-virtual one it is working properly . </p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
class Test
{
public:
   Test() { cout &lt;&lt; "Cons" &lt;&lt; endl;}
   ~Test() {cout &lt;&lt; "Des"&lt;&lt;endl;}
   void *var_ptr;
};
class Test3
{
public:
   Test3() { cout &lt;&lt; "Cons3" &lt;&lt; endl;}
   //virtual ~Test3(){cout &lt;&lt; "Des3" &lt;&lt; endl;};
   ~Test3(){cout &lt;&lt; "Des3" &lt;&lt; endl;};
};
class Test2:public Test3
{
public:
  Test2() { cout &lt;&lt; "Cons2" &lt;&lt; endl;}
 ~Test2() {cout &lt;&lt; "Des2"&lt;&lt;endl;}
};
int  main ()
{
  Test *testPtr = new Test();
 int *ivalue ;
 ivalue = new int;
 testPtr-&gt;var_ptr = (void*)ivalue;
 ((Test2*)(testPtr-&gt;var_ptr))-&gt;~Test2();
}

OutPut :
Cons
Segmentation fault

Without virtual dtor :
Output :
Cons
Des2
Des3
</code></pre>

<p>I can't avoid to use the type-casted pointer (Test2*) as far as my code . I want to understand why I'm getting a seg fault here while using virtual destructor . And is there any other way of type-casting the pointer to make it correct. </p>
