473,385 Members | 1,930 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.

How to release memory of a static pointer data member

Hi, everyone, below is my program to test static pointer data member;

class A
{
private:
static A* p;
protected:
A() {}
public:
static A* init()
{
p = new A();
}
};

A* A::p= NULL;

int main()
{
A* a = A::init();
}

so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory of
the static pointer data member?

Oct 5 '06 #1
6 14339
static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.

On Oct 5, 5:24 am, "laikon" <lai...@gmail.comwrote:
Hi, everyone, below is my program to test static pointer data member;

class A
{
private:
static A* p;
protected:
A() {}
public:
static A* init()
{
p = new A();
}

};A* A::p= NULL;

int main()
{
A* a = A::init();

}so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory of
the static pointer data member?
Oct 5 '06 #2
might be wrong, but you could add function to destroy your pointer
before existing the main
class A
{
......
static void destroy(){
if (p != 0){
delete p;
p = 0;
}
}

.....
};

int main(int argc, char* argv[])
{
A* a = A::init();
A::destroy();
return 0;
}

I checked the updated version using NuMega Bound checker and it did not
complain.
when I ran you version, it compalins about detecting 1 byte of memory
leak

Good luck,

Osama

Ra*********@seznam.cz wrote:
static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.

On Oct 5, 5:24 am, "laikon" <lai...@gmail.comwrote:
Hi, everyone, below is my program to test static pointer data member;

class A
{
private:
static A* p;
protected:
A() {}
public:
static A* init()
{
p = new A();
}

};A* A::p= NULL;

int main()
{
A* a = A::init();

}so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory of
the static pointer data member?
Oct 5 '06 #3
Lots of thanks for your help. But the aim I am look for is to find some
automatic mechanism to release the memory for static data member, just
as destructor does. By your method, clients have to call the function
destroy manually. However, bounds checker does not complain memory
leakage any longer.
Osama Aouda wrote:
might be wrong, but you could add function to destroy your pointer
before existing the main
class A
{
.....
static void destroy(){
if (p != 0){
delete p;
p = 0;
}
}

....
};

int main(int argc, char* argv[])
{
A* a = A::init();
A::destroy();
return 0;
}

I checked the updated version using NuMega Bound checker and it did not
complain.
when I ran you version, it compalins about detecting 1 byte of memory
leak

Good luck,

Osama

Ra*********@seznam.cz wrote:
static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.

On Oct 5, 5:24 am, "laikon" <lai...@gmail.comwrote:
Hi, everyone, below is my program to test static pointer data member;
>
class A
{
private:
static A* p;
protected:
A() {}
public:
static A* init()
{
p = new A();
}
>
};A* A::p= NULL;
>
int main()
{
A* a = A::init();
>
}so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory of
the static pointer data member?
Oct 5 '06 #4
laikon wrote:
Hi, everyone, below is my program to test static pointer data member;

class A
{
private:
static A* p;
protected:
A() {}
public:
static A* init()
{
p = new A();
}
};

A* A::p= NULL;

int main()
{
A* a = A::init();
}

so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory of
the static pointer data member?
Note: you don't really have a memory leak but you have a destruction
failure. Memory is not leaked since C++ does not have a way to return it to
the OS anyway. Thus whether you keep the allocated memory around or free it
does not make an observable difference after the end of main when no
further allocations occur. However, whether the destructor for the pointee
of type A is called or not may make a difference.
You could try a variation of:

#include <iostream>

class A {

struct B {

A* ptr;

B ( void )
: ptr ( new A )
{}

~B ( void ) {
delete ptr;
}

};

public:

static
A* init ( void ) {
static B dummy;
return dummy.ptr;
}

A ( void ) {
std::cout << "constructed\n";
}

~A ( void ) {
std::cout << "destroyed\n";
}

};

int main ( void )
{
A* a = A::init();
}
Best

Kai-Uwe Bux
Oct 5 '06 #5
class A
{
private:
static A* p;
. . .
};

A* A::p= NULL;

so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory of
the static pointer data member?
I tend to use std::auto_ptr for this. So it would look like:
// In .h file
#include <memory>
static std::auto_ptr<Ap;

// In .cpp file
using std::auto_ptr;
auto_ptr<AA::p(NULL);

// In init
p.reset(new A);

Then it magically cleans up when the program ends.

Michael

Oct 5 '06 #6
To me it looks like a singleton approach. Right?

Why don't you use something like:

class A
{
private:
static std::auto_ptr< A p;
protected:
A() {}
public:
static A* init()
{
if( !p.get())
p.reset( new A());
return p.get();
}
};
std::auto_ptr< A A::p;

int main()
{
A* a = A::init();
return 0;
}

This would ensure a) A::p is never created twice regardless how often
A::init is called and b) the destructor would be called at the end of the
program (after! main has terminated).

regard
Juergen

"laikon" <la****@gmail.comschrieb im Newsbeitrag
news:11**********************@e3g2000cwe.googlegro ups.com...
Lots of thanks for your help. But the aim I am look for is to find some
automatic mechanism to release the memory for static data member, just
as destructor does. By your method, clients have to call the function
destroy manually. However, bounds checker does not complain memory
leakage any longer.
Osama Aouda wrote:
>might be wrong, but you could add function to destroy your pointer
before existing the main
class A
{
.....
static void destroy(){
if (p != 0){
delete p;
p = 0;
}
}

....
};

int main(int argc, char* argv[])
{
A* a = A::init();
A::destroy();
return 0;
}

I checked the updated version using NuMega Bound checker and it did not
complain.
when I ran you version, it compalins about detecting 1 byte of memory
leak

Good luck,

Osama

Ra*********@seznam.cz wrote:
static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.

On Oct 5, 5:24 am, "laikon" <lai...@gmail.comwrote:
Hi, everyone, below is my program to test static pointer data member;

class A
{
private:
static A* p;
protected:
A() {}
public:
static A* init()
{
p = new A();
}

};A* A::p= NULL;

int main()
{
A* a = A::init();

}so the problem is memory allocated by init for p is leaked after
function main exit, then where is the proper place to release memory
of
the static pointer data member?


Oct 5 '06 #7

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

Similar topics

5
by: JustSomeGuy | last post by:
I have a class with a private data member and I want to access it from a friend function, but Visual Studio 2003 .NET won't let me. class MyClass { private: static int level; public: friend...
2
by: wkaras | last post by:
Section 9.4.2 paragraph 4 of the draft Standard says: If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
10
by: Jeffrey | last post by:
My understanding is that if you write class X { int y; static int z; }; then you've defined (and declared) X and y, but you have only declared (and not defined) z. If you'd like to...
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: 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...
0
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...
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,...
0
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...

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.