473,856 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14430
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.c omwrote:
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*********@sez nam.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.c omwrote:
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*********@sez nam.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.c omwrote:
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<A p;

// 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.c omschrieb im Newsbeitrag
news:11******** **************@ e3g2000cwe.goog legroups.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*********@sez nam.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.c omwrote:
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
1959
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 ostream & operator<<(ostream & x, const MyClass & c); }
2
3248
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- initializer which shall be an integral constant expression (expr.const) In that case, the member can appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the...
7
3047
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 is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
15
7883
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
2538
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 actually define z, you also need to add
0
9762
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11056
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10696
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10384
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5761
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4174
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3201
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.