Re: Reusing a deleted pointer.

From: Alan (alan_at_surfbest.net)
Date: 12/20/04


Date: Mon, 20 Dec 2004 07:38:15 -0500


"Alan" <alan@surfbest.net> wrote in message news:10sdfhcan0ti0b0@news.supernews.com...
>
> "Dave Townsend" <datownsend@comcast.net> wrote in message news:M4udnWPENK2NG1ncRVn-oA@comcast.com...
> >
> > "Alan" <alan@surfbest.net> wrote in message
> > news:10s8qs23g9vbd17@news.supernews.com...
> [snip]
> > To follow up on the other replies, if you look more carefully at the
> > contents of the object "b" you
> > will see ( at least on VC++6) that after deletion, the memory used by b is
> > written over and the original
> > value a is destroyed! I tinkered a bit with this program, if you do a few
> > more allocations of A's, you
> > will eventually find that an object is allocated at the same address as b,
> > very nasty, now modifying b modifies
> > some other unrelated object! . Just goes to show, just because you don't
> > crash, all may not be well.
> >
> > By the way, your object is too simple to cause a crash, the original
> > address of b would almost always point to to valid memory so deferencing it
> > would not cause a crash after
> > you delete it - just put some virtual functions in your class and try
> > calling one of those after deletion and then
> > you'll crash for sure..!
> >
> > dave
>
> I've put a virtual function in but it didn't fail until I introduced a static
> variable into the class for instance counting. I've cut and cut the
> original program.. The code below (I'm using VC++6 too) succeeds
> or fails, per user choice. What puzzles me is why one class fails and
> the other doesn't - as the differences appear small.
>
> #include <iostream>
> using namespace std;
>
> class Failure {
> int a;
> static int count; // count instances, display them
> public:
> Failure(): a(0) { cout << "constructor " << count++ << endl; }
> ~Failure() { cout << "destructor " << --count << endl; }
> virtual void set(const int& aa) { a=aa; }
> };
>
> class Success {
> int a;
> static int count; // count instances, no display
> public:
> Success(): a(0) { count++; cout << "constructor ?" << endl; }
> ~Success() { --count; cout << "destructor ?" << endl; }
> virtual void set(const int& aa) { a=aa; }
> };
>
> int Failure::count = 1;
> int Success::count = 1;
>
> int main(int argc) {
>
> // no command line arguments (succeeds)
> if( argc == 1 )
>
> Success* s = new Success;
> delete s;
> cout << "class Success *s: try { s->set(1000) }" << endl;
> try {
> s->set(1000);
> } catch(...) { cout << "try failed" << endl; exit(-1); }
> }
>
> // one or more command line arguments (fails)
> else {
> Failure* f = new Failure;
> delete f;
> cout << "class Failure *f: try { f->set(1000) }" << endl;
> try {
> f->set(1000);
> } catch(...) { cout << "try failed" << endl; exit(-1); }
> }
>
> cout << "No failure" << endl;
> return 0;
> }
>
> Regards,
> Alan

There is an opening brace missing after "if( argc == 1 )",
should read "if(argc == 1) {"
I can't explain it - it is there in the original file ;-)



Relevant Pages

  • Re: make a program that count lines in a text
    ... int strlen(); // in strings.h but save time reading whole header ... If you are expecting a text file, open it with "r" on Windows, Linux ... The above test fails to detect empty lines-. ...
    (comp.lang.c)
  • Re: [RFC] Linux Kernel Dump Test Module
    ... Please find below a patch for a simple module to test Linux Kernel Dump ... This module uses jprobes to install/activate pre-defined crash ... Dump Test Tool by Fernando. ... +struct block_device *bdev, unsigned int cmd, ...
    (Linux-Kernel)
  • Re: QASetWindowsJournalHook by managed code
    ... I found something wrong in my application, in fact it didn't really work, why I didn't see crash that is because the callback didn't assign correctly when using GCHandle. ... unsafe public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); ... unsafe public static GCHandle gc1 = GCHandle.Alloc(myHookMessage, ...
    (microsoft.public.dotnet.framework.compactframework)
  • Process just disappears on Access Violation; no crash dump produced
    ... processes recently experience a software error leading to a crash. ... The software platform that I am using is Windows XP x64 Professional ... RIP register immediately produces Access Violation Exception when next ... int func_a ...
    (microsoft.public.vc.debugger)
  • Re: Number of Open Files
    ... that would verify the reason why fopen fails. ... Concerning the fopen level, the macro FOPEN_MAX should give the number of ... guaranteed to contain useful information, ... int GetNumberOfOpenFiles() ...
    (microsoft.public.vc.language)