473,796 Members | 2,720 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot delete created library object

Hi,

in my source code I've included a library I've not written and source code
is available.

In a function, I've generate an object of that library class dynamically
with "new" and at the function's end I want to freed memory for that
object with "delete". However, doing that seem to be not allowed since
I get the compiler error:

error: `virtual LibraryClass::~ LibraryClass()' is protected
myfile.cc:1000: error: within this context

Any ideas what to do in order to avoid memory leaking?

Regards,
Chris

Jul 16 '06 #1
7 1925
* Christian Christmann:
>
in my source code I've included a library I've not written and source code
is available.

In a function, I've generate an object of that library class dynamically
with "new" and at the function's end I want to freed memory for that
object with "delete". However, doing that seem to be not allowed since
I get the compiler error:

error: `virtual LibraryClass::~ LibraryClass()' is protected
myfile.cc:1000: error: within this context

Any ideas what to do in order to avoid memory leaking?
Check how objects of that class are meant to be deleted. Some
possibilities for a meaningful design are:

* A free function 'destroy', declared as a 'friend'.

* A smart-pointer class such as 'std::auto_ptr' , again, declared
as a 'friend'.

* A static member function, e.g. 'LibraryClass:: destroy'.

* A non-static member function, e.g. 'pObject->destroy()' or
'pObject->release()'.

* Automagical clean-up through external means, e.g. a call to a
function 'destroyThemPes kyObjects' which destroys all LibraryClass
objects allocated since the last such call.

--
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?
Jul 16 '06 #2
On Sun, 16 Jul 2006 11:58:27 +0200, "Alf P. Steinbach"
<al***@start.no wrote:
>* Christian Christmann:
>>
in my source code I've included a library I've not written and source code
is available.

In a function, I've generate an object of that library class dynamically
with "new" and at the function's end I want to freed memory for that
object with "delete". However, doing that seem to be not allowed since
I get the compiler error:

error: `virtual LibraryClass::~ LibraryClass()' is protected
myfile.cc:1000 : error: within this context

Any ideas what to do in order to avoid memory leaking?

Check how objects of that class are meant to be deleted. Some
possibilitie s for a meaningful design are:

* A free function 'destroy', declared as a 'friend'.

* A smart-pointer class such as 'std::auto_ptr' , again, declared
as a 'friend'.

* A static member function, e.g. 'LibraryClass:: destroy'.

* A non-static member function, e.g. 'pObject->destroy()' or
'pObject->release()'.

* Automagical clean-up through external means, e.g. a call to a
function 'destroyThemPes kyObjects' which destroys all LibraryClass
objects allocated since the last such call.
* the object isn't intended to be new-ed and deleted at all by the
user. It's either meant to be a stack object or created and managed
(owned) by another object. Anyway, it seems that the user is not in
charge of resource management for that object. The latter is a feature
of any good C++ library.

Best regards,
Roland Pibinger
Jul 16 '06 #3
* Roland Pibinger:
On Sun, 16 Jul 2006 11:58:27 +0200, "Alf P. Steinbach"
<al***@start.no wrote:
>* Christian Christmann:
>>in my source code I've included a library I've not written and source code
is available.

In a function, I've generate an object of that library class dynamically
with "new" and at the function's end I want to freed memory for that
object with "delete". However, doing that seem to be not allowed since
I get the compiler error:

error: `virtual LibraryClass::~ LibraryClass()' is protected
myfile.cc:100 0: error: within this context

Any ideas what to do in order to avoid memory leaking?
Check how objects of that class are meant to be deleted. Some
possibilitie s for a meaningful design are:

* A free function 'destroy', declared as a 'friend'.

* A smart-pointer class such as 'std::auto_ptr' , again, declared
as a 'friend'.

* A static member function, e.g. 'LibraryClass:: destroy'.

* A non-static member function, e.g. 'pObject->destroy()' or
'pObject->release()'.

* Automagical clean-up through external means, e.g. a call to a
function 'destroyThemPes kyObjects' which destroys all LibraryClass
objects allocated since the last such call.

* the object isn't intended to be new-ed and deleted at all by the
user. It's either meant to be a stack object or created and managed
(owned) by another object. Anyway, it seems that the user is not in
charge of resource management for that object. The latter is a feature
of any good C++ library.
Roland, unless this is a class you have designed yourself, where I think
anything would go, please do stop posting misleading nonsense in the
form of statements of fact: post the nonsense as questions (like, "could
perhaps...?") if you absolutely must. Hint why your statement is
nonsense: making the destructor inaccessible is a common technique for
/preventing/ stack allocation, as well as other non-dynamic allocation.
TIA for not posting your nonsense as statements of fact in the future.

--
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?
Jul 16 '06 #4
"Christian Christmann" <pl*****@yahoo. deschrieb im Newsbeitrag
news:44******** *************** @newsread2.arco r-online.net...
Hi,

in my source code I've included a library I've not written and source code
is available.

In a function, I've generate an object of that library class dynamically
with "new" and at the function's end I want to freed memory for that
object with "delete". However, doing that seem to be not allowed since
I get the compiler error:

error: `virtual LibraryClass::~ LibraryClass()' is protected
myfile.cc:1000: error: within this context

Any ideas what to do in order to avoid memory leaking?
As Alf already told you -- read how those objects are supposed to use.

If the destructor is protected, it can only use by members of that class, by
members of a derived class or by friends. So additionally to the points Alf
already mentioned, pehaps the object is not intended to be used directly and
you are supposed to create your own class derived from that one in the
library. However, the usual way to force deriving another class is using
pure virtual functions in the base class or making all its ctors protected,
but a protected dtor also prevents direct use of such a class. So, read the
docs.

HTH
Heinz

Jul 16 '06 #5
On Sun, 16 Jul 2006 11:33:52 +0200, Christian Christmann
<pl*****@yahoo. dewrote:
>in my source code I've included a library I've not written and source code
is available.
In a function, I've generate an object of that library class dynamically
with "new" and at the function's end I want to freed memory for that
object with "delete". However, doing that seem to be not allowed since
I get the compiler error:

error: `virtual LibraryClass::~ LibraryClass()' is protected
myfile.cc:1000 : error: within this context
There are two common situations where this may arise. Since I was
deliberately misinterpreted in a subtread I give two examples of what
might have happened.

1. The class uses 'implementation ' inheritance. To prevent derived
objects from being used polymorphically the base class destructor (and
constructor) are made protected. Implememtaton inheritance is
typically used for value types, i.e. small objects with value
semantics that are created on the stack:

class ValueImp{
protected:
ValueImp() {}
~ValueImp() {}
// ...
};

class Value : public ValueImp{
};

int main() {
ValueImp* p = new Value();
delete p; // error
}

2. The dynamically created object is managed (created, owned, deleted)
by another object. This idiom simplifies resource management issues
because allocation and deallocation disappear from the surface level
of your code. Again, a protected or private destructor inhibits
unintended behavior:

class Manager;

class ManagedObject {
protected:
~ManagedObject( ) {}
friend class Manager;
};

class Manager {
public:
Manager() {}
~Manager() {
while (!container.emp ty()) {
ManagedObject* tmp = container.back( );
container.pop_b ack();
delete tmp;
}
}

ManagedObject* create() {
ManagedObject* p = new ManagedObject() ;
container.push_ back (p);
return p;
}
private:
std::vector<Man agedObject*cont ainer;
// non copyable
Manager (const Manager&);
Manager& operator= (const Manager&);
};
int main() {
Manager m;
ManagedObject* p = m.create();
delete p; // error
}

Best wishes,
Roland Pibinger
Jul 16 '06 #6
Hi

Alf P. Steinbach wrote:
* Roland Pibinger:
>* the object isn't intended to be new-ed and deleted at all by the
user. It's either meant to be a stack object or created and managed
(owned) by another object. Anyway, it seems that the user is not in
charge of resource management for that object. The latter is a feature
of any good C++ library.

Roland, unless this is a class you have designed yourself, where I think
anything would go, please do stop posting misleading nonsense in the
form of statements of fact: post the nonsense as questions (like, "could
perhaps...?") if you absolutely must.
Sorry, but the "*" at the beginning of the paragraph imho clearly indicates
that he only wanted to add another possibility (that you forgot to mention)
to your list. (So, calm down :-) It's healthier anyway)

Furthermore, I think it's the most likely possibility. Objects of that class
maybe should not be created by the user, but should be obtained by other
means (some kind of factory).

Markus
Jul 16 '06 #7
* Markus Moll:
Hi

Alf P. Steinbach wrote:
>* Roland Pibinger:
>>* the object isn't intended to be new-ed and deleted at all by the
user. It's either meant to be a stack object or created and managed
(owned) by another object. Anyway, it seems that the user is not in
charge of resource management for that object. The latter is a feature
of any good C++ library.
Roland, unless this is a class you have designed yourself, where I think
anything would go, please do stop posting misleading nonsense in the
form of statements of fact: post the nonsense as questions (like, "could
perhaps...?" ) if you absolutely must.

Sorry, but the "*" at the beginning of the paragraph imho clearly indicates
that he only wanted to add another possibility (that you forgot to mention)
to your list. (So, calm down :-) It's healthier anyway)

Furthermore, I think it's the most likely possibility. Objects of that class
maybe should not be created by the user, but should be obtained by other
means (some kind of factory).
Regarding the possibility of "it" being a "stack object", the 'auto'
keyword in current C++: no, an inaccessible destructor disallows that.

Regarding the possibility of such objects being restricted to dynamic
allocation (as they are, unless one derives from the class) and
allocation via factory functions, no, in that case it's not reasonable
to use factory functions. Factory functions would just duplicate the
constructor functionality of the class. Avoiding that redundance --
i.e. avoiding factory functions -- is much of the point of using an
inaccessible destructor.

I agree that the "*" probably meant an additional point, and I
misinterpreted it. But that doesn't fix the point(s) being expressed.

--
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?
Jul 16 '06 #8

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

Similar topics

9
715
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit anyway).... Private Sub LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) If e.ChangeType = WatcherChangeTypes.Created Then
1
3849
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
11
1793
by: DamonChong | last post by:
Hi, I am new to c++. I recently spend an enormous among of time troubleshooting a seeminly innocuous piece of code. Although I narrow down this piece of code as the culprit but I don't understand why. Can some guru help to enlighten me? Thank you. // I created an array of pointers to object pointers: Object ** obs = new Object * ;
9
10667
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the table from which I want to delete has two foreign keys that references two other tables and it is also referenced by another table. But this shouldn't be a problem, since I set the commit mode to none (or *none) at all places where this makes...
3
13755
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
26
2892
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37: error: using obsolete binding at 'j'
30
3857
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at the end of the app for example: class test { public: int x;
8
20436
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each class to the type of the object to be created. Here it is the code, which is a modification of the wikipedia C++ factory example code: ----------------------------------8<--------------------------------
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.