How to release memory of a static pointer data member

L

laikon

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?
 
R

RadimBurget

static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.
 
O

Osama Aouda

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
 
L

laikon

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 said:
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

static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.
 
K

Kai-Uwe Bux

laikon said:
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
 
M

Michael

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<A> p;

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

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

Then it magically cleans up when the program ends.

Michael
 
J

Juergen Busch

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 said:
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 said:
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

static A* init()
{
p = new A();
return p;
}
You have forgotten to return A* on new object in your code.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,801
Messages
2,569,658
Members
45,418
Latest member
kiaan_shah

Latest Threads

Top