1

i am getting glibc detected in the following code can someone please explain it to me

#include<iostream>
using namespace std;
class Sample
{
public:
       int *ptr;
       Sample(int i)
       {
           ptr = new int(i);
       }
       void PrintVal()
       {
       cout << "The value is " << *ptr<<endl;
       }
       ~Sample()
       {
           cout<<"CALLED\n";
            delete ptr;
       }
};
void SomeFunc(Sample x)
{
    cout << "Say i am in someFunc " << endl;
    x.PrintVal();
    //cout<<*(s1.ptr)<<endl;
}
int main()
{
Sample s1=10;
cout<<*(s1.ptr)<<endl;
SomeFunc(s1);
cout<<"HERE\n";
cout<<*(s1.ptr)<<endl;
}

Here on calling cout<<*(s1.ptr)<<endl; the glib is detected. what i am not able to understand is why even when desructor is not called for s1 the reference is getting deleted.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
Tilak Raj Singh
  • 353
  • 2
  • 5
  • 15
  • 7
    Double-delete due to not following the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Mat May 05 '13 at 19:49
  • 1
    Tilak, the error message you're seeing says something like "glibc detected [some problem] ..." - It's [the problem] you should be noting in your question, not "glibc detected". Glibc is the thing that detected the problem. Glibc isn't the problem. – Mat May 05 '13 at 19:50
  • yeah sorry Its "./a.out: double free or corruption (fasttop)" what does it exactly mean???? – Tilak Raj Singh May 05 '13 at 19:52
  • It means you free'd (or deleted) the same object twice. See the link in my first comment for the 'fundamental' issue with your code. – Mat May 05 '13 at 19:56

2 Answers2

6

The problem is that you don't have a copy constructor and you have a dynamically allocated member data.

void SomeFunc(Sample x)

creates a new copy of s1. And x and s1 's ptrs will be pointing to the same location. Once the function SomeFunc returns that memory is deleted (when x is destroyed.)

And when main returns s1 is destroyed. Now your destructor tries to delete a memory location which is already deleted and you have double free or corruption error.


A simple copy constructor for your case

Sample(const Sample& s)
{
    ptr = new int(*(s.ptr));
}

You don't really seem to use pointers and dynamic allocation here. However if you have to use dynamic allocation consider taking a look at Rule of three and smart pointers.

Community
  • 1
  • 1
stardust
  • 5,918
  • 1
  • 18
  • 20
  • so will the *ptr of both the classes will point to the memory location where int i is stored???? Or exactly where will the pointers will be pointing to??? – Tilak Raj Singh May 05 '13 at 19:56
  • 1
    @TilakRajSingh `x.ptr` and `s1.ptr` will be pointing to the same exact location. Because when `x` is create from `s1` the value of the pointer is copied directly – stardust May 05 '13 at 19:58
0

For any class that inludes resources that need to be released in the destructor, it's best practice to create a copy constructor for that class as well as an assignment operator to avoid problems like these.

That said, there would have been no need for the copy to occur in the first place, if you had declared the SomeFunc function to take a const reference, which would also be far more effecient.

void SomeFunc(const Sample &x) {
   ...
}
James Holderness
  • 22,721
  • 2
  • 40
  • 52