473,545 Members | 1,972 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 1909
* 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
3832
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...
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,...
11
1769
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
10609
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...
3
13728
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...
26
2858
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
3791
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
20399
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: ...
0
7484
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...
0
7415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7775
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...
1
5344
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...
0
4963
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...
0
3470
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...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.