Re: Allowing at most 2 instances

From: Leor Zolman (leor_at_bdsoft.com)
Date: 05/11/04


Date: Tue, 11 May 2004 01:27:32 GMT

On Tue, 11 May 2004 00:44:16 GMT, "Barry Hynes" <base@islandtelecom.com>
wrote:

>thanks for the help...
>
>i had a couple of cout statements before and after the try.
>
>however still no luck
>
>i figured it might have to do with memory cleanup...but i am not really sure
>what i am suppose to release.
>
>Company2::~Company2(){
>
>delete staff_; //release the pointer to Employee
>delete onlyInstance1_;
>delete onlyInstance2_;
>//should i include onlyInstance1_ and _2 = 0;
>}
>
>to call it
>Company2::instance1()->~Company2()
>
>
>thank again

Remember that, normally, there's one destructor call to match each
constructor call. Your Company2 constructors are coded so that they only
allocate memory for "staff_", so that's all your destructors should be
dealing with. By putting those delete statements for onlyInstance1 and
onlyInstance2 into your Company2 destructor, you're applying delete to the
same pointer more than once (undefined behavior).

You need some sort of static cleanup routine to delete those instance
pointers; perhaps just a
        static cleanup();
member function you invoke at the end of the run.

Instead of this "instance1" and "instance2" business, what about letting
the client manage the pointers (rather than trying to keep them
encapsulated in the class), and just keeping a count of how many have been
created? Heck, let the clients delete their /own/ pointers! ;-)

If you really want to manage them yourself, I'd prefer an array of them to
standalone instances, and perhaps let the factory function accept an index
number (just between 1-2, or 0-1, or whatever, if you have only two, but
then you can up that number easily later on if you choose to.)
        -leor

        -leor

>
>Barry
>"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
>news:4CsxeAW8UBoAFws$@robinton.demon.co.uk...
>> In message <%dTnc.38056$Np3.1491598@ursa-nb00s0.nbnet.nb.ca>, Barry
>> Hynes <base@islandtelecom.com> writes
>> >Good Day Folks,
>> >
>> >been working on this example for about 2 weeks now and still goin
>nowhere...
>> >tryin to create a program that allows me to create at most 2 instances of
>> >Company2.
>> >i think my problem may be somewhere in e3.8-Company2.cpp...not sure
>> >when i run it, the program hangs...
>>
>> Use a debugger to step through the program. Or if you do not have one,
>> insert some diagnostic output to narrow down what it is doing just
>> before it hangs.
>>
>> However those delete in main look deeply suspicious (as do the deletes
>> elsewhere in your code). Delete belongs in a dtor, or at worst at the
>> same code level as the corresponding new.
>>
>>
>> --
>> Francis Glassborow ACCU
>> Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
>> For project ideas and contributions:
>http://www.spellen.org/youcandoit/projects
>

-- 
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix  
C++ users: download BD Software's free STL Error Message Decryptor at:
   www.bdsoft.com/tools/stlfilt.html


Relevant Pages

  • Re: Memory leak
    ... >> memory by the time it finishes execution. ... > Yes, without a destructor, how is the memory you allocated in the ... any class with pointers must have a destructor and copy ... There's nothing in the code that John posted that indicated he must use new ...
    (comp.lang.cpp)
  • Re: Is this math test too easy?
    ... communications glitch; one of the more laughable cartoons ... (A note on "in memory", which for virtual memory machines can get ... Or one can interpret the character string as one of the values ... DOS allowed NULL pointers; the value was in fact interpreted as ...
    (sci.math)
  • [PATCH 2.6.17-rc4 2/6] Some documentation for kmemleak
    ... +Kernel Memory Leak Detector ... +with the difference that the orphan pointers are not freed but only ... +An allocated block of memory is considered orphan if a pointer to its ... The memory allocations hook stores the pointer address together ...
    (Linux-Kernel)
  • [PATCH 2.6.17-rc5 2/7] Some documentation for kmemleak
    ... +Kernel Memory Leak Detector ... +with the difference that the orphan pointers are not freed but only ... +An allocated block of memory is considered orphan if a pointer to its ... The memory allocations hook stores the pointer address together ...
    (Linux-Kernel)
  • [PATCH 2.6.17-rc5 2/8] Some documentation for kmemleak
    ... +Kernel Memory Leak Detector ... +with the difference that the orphan pointers are not freed but only ... +An allocated block of memory is considered orphan if a pointer to its ... The memory allocations hook stores the pointer address together ...
    (Linux-Kernel)