473,385 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Preventing delete on a SmartPointer

Hi,

I have implemented a SmartPointer class following the implementation
proposed by Bill Hubauer(*). But I also override the operator * ()

template<class ObjectType>
class SmartPointer
{
public:
operator ObjectType * () const
{ return Pointer; }
....
private:
ObjectType* Pointer;
};

Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?

Thanks,
Mathieu

(*)
http://groups.google.com/group/comp....3ddc38a827a930

Full source code is at:
http://svn.sourceforge.net/viewcvs.c...ter.h?view=log

Jun 17 '06 #1
15 1986
In article <11**********************@h76g2000cwa.googlegroups .com>,
"mathieu" <ma***************@gmail.com> wrote:
Hi,

I have implemented a SmartPointer class following the implementation
proposed by Bill Hubauer(*). But I also override the operator * ()

template<class ObjectType>
class SmartPointer
{
public:
operator ObjectType * () const
{ return Pointer; }
...
private:
ObjectType* Pointer;
};

Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?


Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().
Jun 17 '06 #2
Daniel T. wrote:
SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?


Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Of course that fix is the best style. (It's why the C++ Standard Library has
few if no conversion operators.)

But can't the templated class also provide an operator delete? Then it could
correctly zilch its owning pointer.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 17 '06 #3

Daniel T. wrote:
Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack.
I thought there could be another way...

-M

Jun 17 '06 #4
* Phlip:
Daniel T. wrote:
SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?

Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Of course that fix is the best style. (It's why the C++ Standard Library has
few if no conversion operators.)

But can't the templated class also provide an operator delete? Then it could
correctly zilch its owning pointer.


An 'operator delete' in SmartPointer would be used in an expression like

delete &s;

not in

delete s;
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 17 '06 #5
On 16 Jun 2006 17:56:44 -0700, "mathieu" <ma***************@gmail.com>
wrote:
Daniel T. wrote:
Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack. I thought there could be another way...


It's yet another "smart" pointers pitfall. Just don't use "smart"
pointers and the 'problems' are gone.

Best wishes,
Roland Pibinger
Jun 17 '06 #6
mathieu wrote:
Daniel T. wrote:
Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack.
I thought there could be another way...

-M


Unfortunately you can't make your smart pointer completely fool-proof.
At the very least one can still do the following:

SmartPointer<int> sp(new int(1));
delete sp.operator->();

Ben
Jun 17 '06 #7
benben wrote:
mathieu wrote:
Daniel T. wrote:
Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack.
I thought there could be another way...

-M


Unfortunately you can't make your smart pointer completely fool-proof.
At the very least one can still do the following:

SmartPointer<int> sp(new int(1));
delete sp.operator->();


Yes, and will all know how easily that accidentally happens :)

To shoot yourself in the foot with less typing do

delete &*sp;

Jun 17 '06 #8

override delete operatpr in your class and make it private .

Regards
Mangesh

Jun 19 '06 #9

override delete operatpr in your class and make it private .

Regards
Mangesh

Jun 19 '06 #10
mathieu wrote:
Hi,

I have implemented a SmartPointer class following the implementation
proposed by Bill Hubauer(*). But I also override the operator * ()

template<class ObjectType>
class SmartPointer
{
public:
operator ObjectType * () const
{ return Pointer; }
...
private:
ObjectType* Pointer;
};

Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?


IIRC, the reason it can delete that is because there is exactly one
operator that
returns a pointer-to-object. If you had a private operator void*, the
compiler couldn't
determine which pointer type to cast to. This is good; the only case in
which you
want operator ObjectType* to work is the case where you need an
ObjectType*.
If the conversion context is ambiguous, like 'delete s', the compiler
should tell
you about the ambiguity.

HTH,
Michiel Salters

Jun 19 '06 #11
mathieu wrote:
Hi,

I have implemented a SmartPointer class following the implementation
proposed by Bill Hubauer(*). But I also override the operator * ()

template<class ObjectType>
class SmartPointer ....
Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;

Is there a way to prevent that ?


I may have missed something here. If you haven't overridden
delete then it won't do anything since s is just an object,
not a pointer to a dynamically allocated object, as far as
C++ is concerned. The object your smart pointer "refers" to
won't get deallocated unless your smart pointer implementation
decides to do so.
--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Jun 19 '06 #12
Roland Pibinger wrote:
It's yet another "smart" pointers pitfall. Just don't use "smart"
pointers and the 'problems' are gone.


Better yet, go back to assembly, and then all the manifold problems
with high level languages disappear!

JK

Cheers! --M

Jun 19 '06 #13

mathieu wrote:
template<class ObjectType>
class SmartPointer
{
public:
operator ObjectType * () const
{ return Pointer; }
...
private:
ObjectType* Pointer;
};

Now my compiler can compile:

SmartPointer<Object> s = new Object;
delete s;


1)
your object class should have only private constructors and one or more
public static methods returning a smartPtr.
2)
Or instead of the static method you could define a factory class which
creates instances of your class (by returning a smartptr).
3)
You could make the destructor of Object private so nobody can call
delete on your object pointer.
I assume that your smartPtr is calling Addref() and Release() or
something similar on your object to increase/decrease the reference
count of your object.

Jun 19 '06 #14

mathieu wrote:
Daniel T. wrote:
Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack.
I thought there could be another way...

-M

But your are allowed to create an instance of smartPtr<Object> on the
stack.
I think your object should have only one lifetime philosophy --
reference counted with smart pointers or not.

Jun 19 '06 #15

mathieu wrote:
Daniel T. wrote:
Yes. If you don't explicitly tell the compiler that this is allowed, it
won't allow it. That means remove the operator ObjectType*().


Well there is -at least- another way like defining the ~Object in the
protected section, but doing so would prevent me to define an Object on
the stack.
I thought there could be another way...

-M

But your are allowed to create an instance of smartPtr<Object> on the
stack.
I think your object should have only one lifetime philosophy --
reference counted with smart pointers or not.

Jun 19 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin Lucas-Smith | last post by:
Can anyone provide any suggestions/URLs for best-practice approaches to preventing SQL injection? There seems to be little on the web that I can find on this. Martin Lucas-Smith ...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
2
by: Tobias Kilian | last post by:
Hi Ng ! I wrote a SmartPointer class which holds a doubled linked list to all its copies to ensure that all copies are holding the same pointer, even if one copy changes later. If the last...
1
by: Wayne Aprato | last post by:
Is there an effective method of preventing users from accidentally or maliciously deleting the database file/files from a shared network drive? At the moment, I'm using a batch file to copy the...
6
by: Rey | last post by:
Howdy all. Am attempting to delete a large number of records (123K) from a table using: db.execute "delete from tblname" Then I double check (code below) to see if records still remain as...
1
by: Eike | last post by:
Hi, I am unable to delete a subfolder that I have created programatically. I am using a modification of the apiSHFileOperation by Dev Ashish (http://www.mvps.org/access/api/api0026.htm) to copy...
19
by: Xavier Décoret | last post by:
Hi, I long thought one could not delete a const pointer but the following code compiles under g++ and visual net: class Dummy { public: Dummy() {} ~Dummy() {}
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
2
by: coder_lol | last post by:
MS VS 7.0 happily resolves by SmartPointer and Inheritance, but I got to use another target compiler and it does not accept user conversion for templates. Can I forced a recast somehow? I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.