help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: destructor


From: Paul Pluzhnikov
Subject: Re: destructor
Date: Wed, 01 Aug 2007 22:07:21 -0700
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:

STEVEN MARX wrote:

> Thanks for you help and time.

Please follow up to newsgroup, rather than to personal e-mail,
unless there is a reason to contact me privately.

> Per my original question, this class compiles and runs without error but 
> does not appear to be deleting memory (using top to measure memory use).

This class violates Item 11 of "Effective C++".
I *urge* you to get that book, and read from start to end, before
continuing.

> 
> class Bitmapgrayscalescalable : public Command {
>  public:
>   int type, mode;
>   float xc, yc, bwd, bht;

Classes generally should not publicly expose their implementation
data (without a good reason).

>   char *bitdata;

Ok, so you have a "pointer to array" as your member.

>   Bitmapgrayscalescalable(...
>     {          
>       bitdata = new char[length];
>       for(int i = 0; i < length; i++)
>        bitdata[i] = buffer[i];
>     }

Why not:

        bitdata = new char[length];
        memcpy(bitdata, buffer, length);

Memcpy will likely be more efficient on just about every platform.

>   ~Bitmapgrayscalescalable() {
>     delete [] bitdata;
>   }
> };

There is nothing wrong with the above, provided you do not copy or
assign any instances of that class. If you do, you have a serious
heap corruption problem (see Item 11).

However, since your "bitdata" member is public, perhaps someone
assigns NULL to it prior to destruction? That would explain the leak.

You can also try to create a minimal self-contained example,
demonstrating the problem.

> */Paul Pluzhnikov <ppluzhnikov-nsp@charter.net>/* wrote:

A. Because doing so makes the conversation harder to read.
Q. Why should I not top-post?

Please do not top-post.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]