Hello,

I am getting the follwoing error in my program:

*** glibc detected *** free(): invalid pointer

The program is as follows.

int main()
{
        operation1* sjob1 = new operation1();
        answer* ans1 = sjob1->execute();

        delete sjob1;

        operation2* sjob2 = new operation2();
        answer* ans2 = sjob2->execute();
        return 0;
}

I have the virtual member function execute() of the base class "operation" which is used by the derived classes -"operation1" and "operation2". I am getting th error at the following line in the code.

answer* ans2 = sjob2->execute();

The function execute() is a virtual function and the base class "operation" has all it member functions as virtual. I have tried deleting the object "ans1" also but i get a segmentaion fault if i do that.

Any help is appreciated.

Thanks

Dani AI

Generated

Short diagnosis and likely causes:
"free(): invalid pointer" means the allocator detected a pointer being freed that it doesn't recognize or the heap metadata was corrupted. In C++ that most often comes from one of these: returning a pointer to a stack/local object, deleting the same memory twice, using the wrong deallocation (free vs delete, or delete[] vs delete), or a buffer overflow/heap corruption in a destructor or other routine. Given 's description (first execute works, second fails) and 's comment, the most productive place to look is the implementations of the two execute() methods — especially operation2::execute().

Quick checklist to apply to execute() and answer:

  • Verify what execute() actually returns. It must not return the address of a local (stack) variable or a pointer to storage owned by the operation instance if you delete that instance before using the answer.
  • If ownership is unclear, change the API so ownership is explicit (return by value or a smart pointer).
  • Ensure you are not mixing malloc/free with new/delete, and not deleting with delete[] what was allocated with new (or vice versa).
  • Make the base class destructor virtual if you ever delete derived objects via a base pointer: virtual ~operation() = default;. Missing a virtual destructor can cause undefined behavior.

Practical debugging steps:

  • Compile with debug symbols and run under Valgrind:
    g++ -g -O0 ... && valgrind --tool=memcheck --leak-check=full --track-origins=yes ./yourprog
    Valgrind will point at the exact allocation/free site or report heap corruption.
  • If you can compile with AddressSanitizer: -fsanitize=address -g this often gives an immediate, easy-to-read report.
  • As a fast test, comment out delete sjob1 (or delay it) — if the error disappears, ans1 was pointing into sjob1's storage.

Safer design patterns (recommended):

  • Make execute() return a smart pointer or a value to avoid manual delete bookkeeping:
virtual std::unique_ptr<answer> execute() = 0;

// in derived:
std::unique_ptr<answer> operation1::execute() {
    return std::make_unique<answer>(/*...*/);
}

// usage:
auto ans1 = sjob1->execute(); // automatic lifetime, no manual delete

Also, after any raw delete set the pointer to nullptr to reduce double-delete risk. Following these checks and running Valgrind/ASan will almost always reveal the exact cause.

Recommended Answers

All 6 Replies

What's this?

operstion2

Is it a typo? Is it a global object declared in a file included in the program (ala cout or cin)?

Hi,

Sorry that is a typo. it is operation2

Thanks

Could you explain a bit more what you are trying to do? :)

operation1* sjob = new operation1();
answer* ans1 = operation1->execute();
delete sjob;

shouldn't you operate on sjob since you have created it? creating an object with new and then deleting it just seems like a waste :confused: or am I missing something?

I don't think you can call objects member functions without creating an object first (someone confirm this)

sorry for the incorrect code. I have corrected the code.

Can someone help me with possible cause of errors.

After correction, if this line

answer* ans1 = sjob1->execute();

works, but this line

answer* ans2 = sjob2->execute();

still doesn't work, there has to be an error in your class operation2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.