How to solve memory leak problem? By new and delete operator

I have two classes.
B *ss=new B;
class A:
{
/*Functions prototype*/
Function1();
Function2();
};
/*Function declaration of class A in file 1
Function1()
{
ss->function3;
delete ss;
}

class B:
{
/*Functions prototype*/
Function3();
};
//Function declaration of class A in file 2
Function3()
{
/* some codes*/
}
This type of implementation showing error message: double free or corruption (fasttop): 0x0000000001e25010.
Double free means you are freeing the same memory twice (using delete on the same poiner twice). You must make sure when you free an object that it will not be used later.
By declaring : B *ss; in first class
later in class A function i am trying to call other class function by ss-> function3();
say in function3() i have buffer. During first time execution buffer store values correctly but during second time execution buffer is empty. How to solve this problem?

Why do you delete ss in the functions? Shouldn't you delete it in the destructor instead?
Basically there are 5 diff files, they are main.cpp, file1.h, file1.cpp, file2.h, fil2.cpp.
1] main.cpp
{
/* Calling other two classes and initialization done here*/
}

2] file1.h
class A:
{
A()
{
}
~A()
{
}
Function1();
};

3] file1.cpp
#include"file2.h"
B *ss=new B;
char function1()
{
ss->function2(); // Calling function 2 of class B
delete ss; //return the memory to the operating system. ss pointer is now dangling pointer.
ss=nullptr; //ss is now a null pointer.
}

4] file2.h
class B:
{
B()
{
}
~B()
{
}
Function2();
};

5]file2.cpp
char B::function2()
{
/* Basic function like storing char in buffer which are sent from class A*/
char buffer[255];
memset(buffer,0,256);

}


As you said if i declare ss in destructor of file2.h then it will give error. Here what i am facing problem is in first time execution arrays are sent from class A are store in buffer but in 2nd time buffer value is nil.
}
It's hard to follow your program because it's not real code. It looks like ss is a global variable. Was your intention was to make it a local variable?
No ss is just pointer which holds address of dynamicallally allocated memory location.
B *ss=new B; // dynamically allocate memory and assign address to ss

/*Later in function1 of class A i am calling function 2 of class B using pointer ss*/
function1()
{
ss->function2(str);
delete ss; //return the memory to the operating system. ss pointer is now dangling pointer.
ss=nullptr; //ss is now a null pointer.
}

Say we are sending string from function1 of class A to function2 of class B. My problem is during first time execution that string is coming in the buffer of Function2 of class B. But after that buffer showing nil for next consecutive executions.
Please post code where we can reproduce the problem.

Whitout really getting it: When you set ss to null it will remain null until you reset the variable to something else.

I would suggest to avoid global variables.
Please post code where we can reproduce the problem.

And when you do so, please use code tags to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/
Thank you everyone. Its working now. I made some modification to code.
I made some modification to code.

Oh, well that makes everything so much clearer.
Topic archived. No new replies allowed.