Reputation: 17842
I will explain the scenario using example.
void func()
{
CLine* pObj = new CLine();
}
void func2()
{
}
How to delete the CLine object in func2()? Is it possible?
Thanks
Upvotes: 3
Views: 1629
Reputation: 23115
You have to delete the object you created dynamically in a function inside the function itself. If you leave the function without deleting it, you cannot delete it and the memory is lost.
Upvotes: 0
Reputation: 40613
There is probably no need to use a free-store allocated variable at all in this case. Just do:
void func()
{
CLine obj;
}
void func2()
{
}
obj
will be automatically destroyed at the end of the scope.
If the variable absolutely must be free-store allocated, then you can put it in an unique_ptr as follows:
void func()
{
const std::unique_ptr<CLine> obj(new CLine());
}
void func2()
{
}
if you are using c++03
then just replace unique_ptr
with auto_ptr
.
Anyway, if you really must use the code exactly as it is in your example then there is a way to still delete the CLine
object. You have to use overloaded operator new and operator delete. It would have to be something like:
#include <vector>
#include <new>
#include <algorithm>
struct CLine {
//... Other members ...
static std::vector<void*> pointerCache;
void* operator new(std::size_t size) throw(std::bad_alloc) {
while (true) {
void* pointer(malloc(size));
if (pointer) {
try {
pointerCache.push_back(pointer);
return pointer;
}
catch (std::bad_alloc&) {
free(pointer);
}
}
if (std::new_handler handler = std::set_new_handler(0)) {
std::set_new_handler(handler);
(*handler)();
}
else {
throw std::bad_alloc();
}
}
}
void operator delete(void *p) throw() {
pointerCache.erase(std::remove(pointerCache.begin(), pointerCache.end(), p),pointerCache.end());
free(p);
}
void* operator new(std::size_t size, std::nothrow_t const&) throw() {
try {
return operator new(size);
} catch (std::bad_alloc&) {
return 0;
}
}
//Compressed for space constraints.
void operator delete(void *p, std::nothrow_t const&) throw(){
operator delete(p);}
void* operator new[](std::size_t size) throw(std::bad_alloc){
return operator new(size);}
void operator delete[](void *p) throw(){operator delete(p);}
void* operator new[](std::size_t size, std::nothrow_t const& nothrow) throw(){
return operator new(size, nothrow);}
void operator delete[](void *p, std::nothrow_t const& nothrow) throw(){
operator delete(p, nothrow);}
};
std::vector<void*> CLine::pointerCache;
And then your code could look like this:
void func()
{
CLine* pObj = new CLine();
}
void func2()
{
//The most recently added pointer in CLine::pointerCache is
// the one that was added in func()
delete static_cast<CLine*> (CLine::pointerCache.back());
}
int main()
{
func();
func2();
}
But really, there is so much wrong with this code that you would be better just changing your design. Some of the problems include: the total confusion that you will cause to people reading your code, the lack of thread-safety, and the use of a global pointerCache
object.
Upvotes: 3
Reputation: 6478
Yes you can.
void func()
{
CLine* pObj = new CLine();
}
void func2(CLine* pObj)
{
delete pObj;
}
if you pass CLine pointer to func2
Upvotes: 1
Reputation:
It IS possible if func() and func2() are methods of the same class, and pObj is a member variable of that class. I realize this is not how you structured your question, but this is one way to get the effect you are looking for
class MyClass
{
CLine* pObj;
void func()
{
pObj = new CLine();
}
void func2()
{
delete pObj;
}
}
Upvotes: 1
Reputation: 26863
No, it's not possible. Because the only copy of the pointer is lost when func
goes out of scope, you no longer have a reference to the memory to use with operator delete
.
Upvotes: 0
Reputation: 96251
It's not possible. As soon as func
finishes you've lost all reference to pObj
and leaked its memory.
Upvotes: 13