I have a class with a static factory constructor which returns a pointer to the object created.
I have to declare the object as a static object inside a namespace but I don't know how to delete it correctly
class Foo
{
public:
Foo(int, int* );
virtual ~Foo();
static Foo* MyFooInitializer(int n )
{
int *p = new int[n];
for (int i=0; i<n; i++)
p[i]=i;
Foo *ret = new Foo(n,p);
delete p;
return ret;
}
int someFooFunction(int a);
}
Then in my namespace I have a static inline function
namespace MyNamespace
{
static inline void myfunction()
{
static Foo *foo1 = Foo::MyFooInitializer(10);
int y = somevalue();
int x = foo1->someFooFunction(int y);
}
}
I obviously have a memory leak here because the object is never deleted.
The important fact is that I need that foo1 is declared as static because once created it must be the same object during all the program and must be unique (it keeps track of some variables).
Probably this is a design problem, but I don't know how to delete it when my program exits or when I explicitly want to delete it to reinitialize it.
SOLUTION:
I modified the body of MyFooInitializer this way:
static Foo* MyFooInitializer(int n )
{
int *p = new int[n];
for (int i=0; i<n; i++)
p[i]=i;
static Foo ret = Foo(n,p);
delete[] p;
return &ret;
}
This allows me to release all the memory correctly when the program terminates. Valgrind says all the heap memory is freed!
There is no need for allocating that Foo on the heap here:
static Foo* MyFooInitializer(int x) {
static Foo some_foo(x);
return &some_foo;
}
There are no leaks in that code, that Foo will be destroyed when your program ends.
Note that if the pointer returned by MyFooInitializer actually points to some class which inherits from Foo, then you'd just have to use the derived type for the static variable:
static Foo* MyFooInitializer(int x) {
static SomeFooDerived some_foo(x);
return &some_foo;
}
Edit: Since you provided the actual function body, my answer is valid. You'd do it like this:
static Foo* MyFooInitializer(int n ) {
// Don't know what this p is, anyway...
int *p = new int[n];
for (int i=0; i<n; i++)
p[i]=i;
static Foo ret(n,g); // what is g?
delete[] p; // smart pointer plx
return &ret;
}
How about
static inline void myfunction()
{
static std::unique_ptr<Foo> foo1(Foo::MyFooInitializer(10));
int y = somevalue();
int x = foo1->someFooFunction(int y);
}
If you absolutely need to create the foo1 dynamically, then write an extra class and make it static/global by value. Then use it's destructor to delete object(s).
class MasterControlClass
{
public:
Foo* foo1;
MasterControl(){foo1 = NULL;}
~MasterControl(){delete(foo1), foo1 = NULL;}
};
static inline void myfunction()
{
static MasterControlClass mcp;
mcp.foo1 = Foo::MyFooInitializer(10);
}
In this way, when your program is closing, the mcp desctructor will get called and will do the cleanup.
If you want to reinitialize, then ofc you have to delete foo1 at every assign, just add
if(mcp.foo1)
{
delete mcp.foo1;
mcp.foo1= NULL;
}
Or even better, move it to a method of mcp.
Related
A fairly common thing I need to do is allot an object and some memory it'd like, in a strictly contagious region of memory together:
class Thing{
static_assert(alignof(Thing) == alignof(uint32), "party's over");
public:
~Thing(){
//// if only, but this would result in the equivalent of `free(danglingPtr)` being called
//// as the second stage of shared_ptr calling `delete this->get()`, which can't be skipped I believe?
// delete [] (char*)this;
}
static Thing * create(uint32 count) {
uint32 size = sizeof(Thing) + sizeof(uint32) * count; // no alignment concerns
char * data = new char[size];
return new (data)Thing(count);
}
static void destroy(Thing *& p) {
delete [] (char*)p;
p = NULL;
}
uint32 & operator[](uint32 index) {
assert(index < m_count);
return ((uint32*)((char*)(this + sizeof(Thing))))[index];
}
private:
Thing(uint32 count) : m_count(count) {};
uint32 m_count;
};
int main(){
{
auto p = shared_ptr<Thing>(Thing::create(1));
// how can I tell p how to kill the Thing?
}
return 0;
}
In Thing::Create() this is done with placement new into a section of memory.
I'd also like to have a shared pointer manage it in this case, using auto p = shared_ptr<Thing>(Thing::create(1)). But If it calls the equivalent of delete p.get() when the ref count empties, that'd be undefined as it mismatches the type and, more importantly, mismatches plural new with singular delete. I need it to delete in a special way.
Is there a way to easily set that up without defining an outside function? Perhaps by having the shared pointer call Thing::destroy() when the ref count empties? I know that shared pointer can accept a "deleter" as a template argument, but I'm unsure how to use it, or if it's even the proper way to address this?
std::shared_ptr accepts a deleter function as a second parameter, so you can use that to define how the managed object will be destroyed.
Here's a simplified example:
class Thing
{
public:
~Thing()
{
std::cout << "~Thing\n";
}
static std::shared_ptr<Thing> create() {
char * data = new char[sizeof(Thing)];
Thing* thing = new (data) Thing{};
return std::shared_ptr<Thing>{thing, &Thing::destroy};
}
static void destroy(Thing* p) {
p->~Thing();
delete [] (char*)p;
}
};
int main()
{
auto p = Thing::create();
}
Live Demo
I'm trying to return pointer from function in derived class,
This is the code:
class A
class A {
protected:
C c;
public:
virtual void func(){
unsigned char *data;
int size=getData(data);
}
}
class B
class B : public A {
private:
int size;
public:
B(const C &_c) { c=_c; };
const int B::getData(unsigned char *data) const {
data=(unsigned char *)malloc(size);
memcpy(data,c.getMem(),size);
if(!data){
//err
}
return size;
}
class C
class C {
private:
unsigned char *mem;
public:
C(unsigned char *_mem) : mem(_mem);
const unsigned char *getMem() const { return mem; };
}
main.cpp
C c(...);
B *b=new B(c);
b->func();
The error I get when getData returns
(this=0x50cdf8, data=0x2 <error: Cannot access memory at address 0x2>, size=32)
Thanks.
In class A, the func() is worthless because:
1. size is not returned to the caller.
2. The pointer data is local to func and it's contents will disappear after the end of execution in func().
You don't need to return const int from a function. A function will return a copy of variables, so they are constant (copies).
Don't use malloc in C++, use operator new. The malloc function does not call constructors for objects.
Don't use new or malloc unless absolutely necessary. Usually, dynamic memory is for containers where their capacity is not known at compile time; or for objects that live beyond a function or statement block's execution time. If you must use dynamic memory, use a smart pointer (like boost::shared_ptr).
Use std::vector for dynamic arrays. It works; it's been tested; you don't have to write your own, including memory management.
You are passing data by value (a copy of the pointer). If you want to modify a pointer, pass it by reference or pass a pointer to the pointer; (what I call the address of the pointer).
Example:
void my_function(uint8_t * & p_data)
{
p_data = new uint8_t[512];
}
Or
void another_function(unsigned char * * pp_data)
{
*pp_data = new unsigned char [1024];
}
A much better solution:
void better_function(std::vector<uint8_t>& data)
{
data.reserve(64);
for (uint8_t i = 0; i < 64; ++i)
{
data[i] = i;
}
}
I have a question about delete and memory leak in C++. Considered the code below:
class AnObject{
public:
AnObject* Foo(){
// how can I delete this object ???
AnObject* obj = new AnObject();
...
return obj;
}
};
int main(){
...
AnObject* x = new AnObject();
AnObject* result = x->Foo();
delete x;
return 0;
}
My question is how can I delete the pointer in fuction AnObject::Foo() ???
// I read some suggestions who required changing the function, don't create an object with the word new in a fucntion. But does it exist a method to delete this pointer ?
You would do so by deleting this within AnObject::Foo but I would strongly discourage you from doing so. In this case, you do not need pointers at all:
class AnObject{
public:
AnObject Foo(){
return AnObject{};
}
};
int main()
{
AnObject x{};
AnObject result = x.Foo();
return 0;
}
If for whatever reason you really do need pointers, consider smart pointers
#include <memory>
class AnObject{
public:
std::unique_ptr<AnObject> Foo(){
return std::make_unique<AnObject>();
}
};
int main()
{
std::unique_ptr<AnObject> x = std::make_unique<AnObject>();
std::unique_ptr<AnObject> result = x->Foo();
return 0;
}
In both cases, you do not need to delete anything as you are using RAII semantics to handle your memory cleanup via scope.
In C, in order to assure that all structs are zeroed I wrap malloc with:
void* safe_malloc(size_t size){
void *result = malloc(size);
memset(result, 0, size); //NULL check ommitted for brevity
return result;
}
so that I can avoid doing:
struct Foo {
int bar;
int bat;
};
struct Foo *tmp = malloc(sizeof(struct Foo));
tmp->bar = 0;
tmp->bat = 0; // I want to avoid this laundry list of initializers
I want to do something similar for C++ whereby I want all members of a class to be initialized to zero like what is done automatically in Java. There are two problems that come to mind using a solution like the C solution: 1. you cant clear the vtable ptr and 2. a subclass will clear inherited member values.
class Foo {
public:
int bar;
int bat;
Foo(int bar){
this->bar = bar;
this->bat = 0; // I dont want to explicitly have to initialize this here
}
}
class Baz : public Foo {
public:
int bam;
Baz(int bar, int bam) : Foo(bar) {
this->bam = bam;
}
}
How can I avoid the laundry list equivalent in C++?
Prefer calloc over malloc + memset
To zero the objects, choose your constructors wisely.
You can overload new operator to return zeroed memory. (Overload new for a class or globally as per your requirement) This overloaded new should zero the bytes allocated.
I'm having trouble transferring some data contained in a vector between my functions. The situation is as follows:
void generateObjects(std::vector<MyClass> &objects)
{
objects.clear();
//Fill objects vector
std::vector<MyClass> p;
//This 4-line pattern is repeated a number of times to generate all objects and store them in variable 'objects'
p.clear();
generateSomeOfTheObjects(p); //p is again passed by ref. in/out parameter
for(uint j = 0; j < p.size(); p++){
objects.push_back(p[j]);
}
//Print some members of the objects - works fine
for(uint i = 0; i < objects.size(); i++){
printf("%f ",objects[i].mymember->myElm);
}
}
int main()
{
std::vector<MyClass> objects;
generateObjects(objects);
//Print size of vector - size is correct it is the same as it is in generateObjects func
printf("%lu\n",objects.size());
//Again print members of the objects - some members are retained after the function call, some are lost.
//The one below doesn't work, mymember is a pointer to another object and its member myElm seems not initialized.
for(uint i = 0; i < objects.size(); i++){
printf("%f ",objects[i].mymember->myElm);
}
//Here I need to pass the objects to another read-only function
...
}
I have searched the internet for similar cases and actually found many, but I couldn't apply the same fixes to my code. I'm trying to reach a member of an object pointed to by a member of a MyClass instance (objects[i].mymember->myElm) What possibly am I missing here?
Probably the error lies in the implementation of MyClass. I'd say that this class contains some pointer that is initialized with the address of a local variable, so when you return from some of the functions that pointer points to a destroyed object.
That would undefined behavior but it may work by chance. When you return from the first function, the stack memory is finally overwritten and your data is lost.
UPDATE: Thanks to the insight by #chris in the comments below, the most likely reason is that your MyClass does not have a copy constructor, but it does have a pointer member.
Something like this:
class MyClass
{
public:
Member *mymember;
MyClass()
{
mymember = new Member;
}
~MyClass()
{
delete mymember;
}
};
Now what happens if you use the compiler generated default copy constructor (or the copy operator)?
void foo()
{
MyClass a;
{
MyClass b(a);
}
//a.mymember is no longer valid
}
Both a and b share the same pointer mymember, so when one of them is destroyed, the mymember is deleted and the other one holds a dangling pointer.
That's why we have the rule of three. It states:
Whenever you define a non-default destructor, you most likely will want also a non-default copy-constructor and a non-default copy-operator.
Now you have to decide if you want to share the ownership of the mymember or if you want to copy it. The first one is best done with smart pointers (shared_ptr) and the second one with deep copy.
For example, deep copy:
class MyClass
{
public:
Member *mymember;
MyClass()
{
mymember = new Member;
}
MyClass(const MyClass &c)
{
mymember = new Member(c.mymember);
}
MyClass &operator=(const MyClass &c)
{
if (this != &c) //be aware of self-copy
{
delete mymember;
mymember = new Member(c.mymember);
}
return *this;
}
~MyClass()
{
delete mymember;
}
};
And with shared pointers:
class MyClass
{
public:
std::shared_ptr<Member> mymember; //or boost::shared_ptr if old compiler
MyClass()
:mymember(new Member)
{
}
//no custom-made destructor -> no rule of 3
};
Perhaps unrelated to you question, but this:
void generateObjects(std::vector<MyClass> &objects)
{
objects.clear();
std::vector<MyClass> p;
p.clear();
generateSomeOfTheObjects(p);
for(uint j = 0; j < p.size(); p++){
objects.push_back(p[j]);
}
for(uint i = 0; i < objects.size(); i++){
printf("%f ",objects[i].mymember->myElm);
}
}
Is the same as this:
void generateObjects(std::vector<MyClass> &objects)
{
objects.clear();
generateSomeOfTheObjects(objects);
std::reverse(objects.begin(), objects.end());
for(uint i = 0; i < objects.size(); i++) {
printf("%f ",objects[i].mymember->myElm);
}
}
You copy issues, as #rodrigo mentioned, is that you are not doing deep copies with your copy constructors.