Getting error while making delete operator private

Discussion in 'C++' started by Premal, Aug 13, 2007.

  1. Premal

    Premal Guest

    Hi,

    I tried to make delete operator private for my class. Strangely it is
    giving me error if I compile that code in VC++.NET. But it compiles
    successfully on VC++6.o. Can anybody give me inputs about it. I wanted
    that on my class delete should not work. Object pointer should be
    deleted using my function only which is taking care of reference count
    for particular class.

    Thanx in advance for your inputs.

    Regards,
    Premal Panchal
     
    Premal, Aug 13, 2007
    #1
    1. Advertisements

  2. Premal

    Jim Langston Guest

    Please show what you tried that didn't work.
     
    Jim Langston, Aug 13, 2007
    #2
    1. Advertisements

  3. Premal

    Premal Guest

    Hi,
    I tried following one:

    class RefCountImpl
    {
    private:
    //data members
    protected:
    void operator delete(void*);
    public:
    //methods
    };
    if i have above class implementation.Then in VC++6.0 it works. You can
    allocate memory using new but you cannot delete that pointer in your
    clilent code. You have to use some method provided by above class to
    release the pointer.

    Same thing doesnt work in VC++.NET. It clearly throws error that
    making delete private cause memory leakage. May be VC++.NET compiler
    become more stirct about this. :(.....

    I hope you got my point. I hope you can give me some valuable input.

    Regards,
    Premal Panchal
     
    Premal, Aug 13, 2007
    #3
  4. Premal

    Ron Natalie Guest

    First off, that is not the delete operator (despite it's unfortunate
    name) it is the memory deallocation function. Compilers are free to
    issue whatever diagnostics they want over the required ones.

    Why did you do this?
     
    Ron Natalie, Aug 13, 2007
    #4
  5. Premal

    James Kanze Guest

    The problem is that the new operator must call delete if the
    constructor throws. So you can't use new outside of a member
    function if operator delete is private. Since this behavior was
    added during standardization, some older compilers (e.g. VC++
    6.0) might not implement it.

    The solution, of course, is to provide factory functions as
    well: static member functions which do the new.
     
    James Kanze, Aug 13, 2007
    #5
  6. Premal

    James Kanze Guest

    Yes, but access control for the delete operator is done on the
    memory deallocation function which is associated with it.

    In this case, of course, what's bothering him is that the access
    control for the new operator also checks the memory deallocation
    function (because the new operator must call the memory
    deallocation function if the constructor exits via an
    exception).
    In this case, the compiler is required to issue the diagnostic.
    §5.3.4:

    If the new-expression creates an object or an array of
    objects of class type, access and ambiguity control are
    done for the allocation function, the deallocation
    function (12.5), and the constructor (12.1). If the new
    expression creates an array of objects of class type,
    access and ambiguity control are done for the destructor
    (12.4).
    As he said, he didn't want any one else to delete the class.
    (Think about it for a moment. For most classes that you
    allocate dynamically, anything but a delete this would be an
    error.)
     
    James Kanze, Aug 13, 2007
    #6
  7. Premal

    Jim Langston Guest

    #include <iostream>

    class RefCountImpl
    {
    private:
    //data members
    protected:
    void operator delete(void*) { ::delete this };
    public:
    void Kill() { delete this; }
    //methods};
    };

    int main()
    {
    RefCountImpl Foo;
    RefCountImpl* Bar = new RefCountImpl;
    Bar->Kill(); // Works
    delete Bar; // Doesn't work

    return 0;

    }

    Show how you are trying to use it. It does not fail for me unless I try to
    delete the instance, are you trying to do this? Are you trying to make it
    on the stack? How are you using it? Show the code that is actualy causing
    the error when you compile.
     
    Jim Langston, Aug 13, 2007
    #7
  8. Premal

    Premal Guest


    Hi Jim,

    Sorry for replying late. I was out of the town. I may be not available
    for two days.. But just let me know whether you have tried on VC++6.0
    or VC++.NET. In case of VC++6.0 above one is working. But its not
    working in VC++.NET. May be as James has mentioned that according new
    standard it may be the problem.

    Actually I have to work on both VC++6.0 and VC++.NET. Once I reach at
    my place I will post you exact error. But it is giving error at
    compile time itself in VC++.NET while making delete operator private.

    Really thanx to both of you JIM and JAMES. Thanx for your input. If
    you find something interesting about it then let me know. As I told
    you I am implementing class which is extension to auto_ptr and it
    works on reference count basis. If possible I will try to post you
    whole design of that.

    I hope you post soon something intereting about it.

    Regards,
    Premal
     
    Premal, Aug 14, 2007
    #8
  9. Premal

    Premal Guest

    Hi,

    Jim and James can you give your email address so I can also email you
    document about my development around Reference count object
    implementation.

    Regards,
    Premal Panchal
     
    Premal, Aug 14, 2007
    #9
  10. Premal

    Ron Natalie Guest

    Well, he didn't show any code that actually invoked new or the
    allocation function, so it wasn't clear that such a diagnostic
    was required.
     
    Ron Natalie, Aug 14, 2007
    #10
  11. Premal

    Premal Guest

    Hi Ron,

    Look at the example given by Jim. It was the compilation error in my
    case. I invoke it in similar fashion, but error was that as delete
    can't be private. So I wanted why it is so. It comes only in .NET not
    in VC++6.0.

    I hope you got my point.

    Regards,
    Premal Panchal
     
    Premal, Aug 14, 2007
    #11
  12. Premal

    James Kanze Guest

    The above line shouldn't work.

    The original standard wasn't 100% clear about this; if the
    constructor of RefCountImpl terminates with an exception, then
    the code here must call delete, but in this case, the compiler
    can easily determine that it cannot terminate with an exception.
    Some compilers used this information to avoid requiring that
    operator delete was accessible, and others didn't. (And some
    older compilers, like I think VC++ 6.0, didn't even bother with
    the delete -- a constructor terminates with an exception, and
    you leak memory.) I believe in fact that there was a defect
    report concerning this. At any rate, the current draft
    explicitly says that delete must be accessible here, even if the
    compiler can determine that the constructor can't possibly
    throw. (It's a logical choice, because otherwise, how much
    analysis should the compiler do to determine whether the
    constructor can throw or not.)
     
    James Kanze, Aug 14, 2007
    #12
  13. Premal

    James Kanze Guest

    [...]
    Mine's in my .sig, but as soon as my current data base request
    works, I'm going off line for about three weeks vacation. With
    no computer anywhere in sight.
     
    James Kanze, Aug 14, 2007
    #13
  14. Premal

    Premal Guest

    Hi,

    Sorry folks for troubling you. Finally I have reached at my place and
    i aksed complete code from my junior. Mistake was he has just declared
    the delete operator but didnt implement it.

    Thats why we were getting error that delete is in private mode and
    memory cannot be freed.

    Sorry folks. Now I have changed the code and my design is working.
    Thanks for your support.

    Regards,
    Premal Panchal
     
    Premal, Aug 14, 2007
    #14
  15. Premal

    Premal Guest

    Hi,
    Again sorry.
    I did mistake. I ran that on VC++6.0... On VC++ .NET its still giving
    me error. If I use "new" on RefCountImpl then it clearly says that "
    operator::delete : cant access protected member". I dont know why this
    strange things.

    Sorry again, So pls start thinking about it and let me know if any
    fruitful explaination is there or not.

    If anybody has latest compiler other than .NET then also kindly try
    the code given by JIM and see whether same kind of error there or
    not.

    Also Jim let me know which compiler you have used for your code
    snippet.

    Thanx and Regards,
    Premal Panchal
     
    Premal, Aug 14, 2007
    #15
  16. Premal

    Jim Langston Guest

    The above line shouldn't work.

    The original standard wasn't 100% clear about this; if the
    constructor of RefCountImpl terminates with an exception, then
    the code here must call delete, but in this case, the compiler
    can easily determine that it cannot terminate with an exception.
    Some compilers used this information to avoid requiring that
    operator delete was accessible, and others didn't. (And some
    older compilers, like I think VC++ 6.0, didn't even bother with
    the delete -- a constructor terminates with an exception, and
    you leak memory.) I believe in fact that there was a defect
    report concerning this. At any rate, the current draft
    explicitly says that delete must be accessible here, even if the
    compiler can determine that the constructor can't possibly
    throw. (It's a logical choice, because otherwise, how much
    analysis should the compiler do to determine whether the
    constructor can throw or not.)

    =====

    Well, it compiled in Microsoft VC++ .net 2003. Perhaps it won't compile in
    2005.
     
    Jim Langston, Aug 15, 2007
    #16
  17. Premal

    Premal Guest

    Yes Jim,
    May by you are right. It is not getting compiled. But interesting
    thing is in VC++6.0 if you make delete operator protected and just
    throw exception in constructor. And in your client code under try
    catch block make the array of RefImplCount. In this case if you have
    thrown exception after making two objects in array then it is calling
    delete and hence the destructor of the class. So its very surprising
    that why it is now restricted in VC++.NET 2005.

    I hope you are getting my point.I will check version of .NET at my
    place also.I am not able to find your email address. If you can
    provide it then I will send my code to you so may be you can give
    better suggestion.

    I really appreciated response you have given. Thanx a lot for this.

    Regards,
    Premal Panchal
     
    Premal, Aug 16, 2007
    #17
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.