473,382 Members | 1,407 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,382 software developers and data experts.

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 1901
* 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 'destroyThemPeskyObjects' 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.nowrote:
>* 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 'destroyThemPeskyObjects' 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.nowrote:
>* 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 'destroyThemPeskyObjects' 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.arcor-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.empty()) {
ManagedObject* tmp = container.back();
container.pop_back();
delete tmp;
}
}

ManagedObject* create() {
ManagedObject* p = new ManagedObject();
container.push_back (p);
return p;
}
private:
std::vector<ManagedObject*container;
// 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
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...
1
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...
11
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...
11
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...
9
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...
3
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....
26
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:...
30
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...
8
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.