First of all, you are using new and free together. That is a very bad idea because the destructor will not be called in general. You should replace free with delete.
Moreover, you are getting the ownership for that return value of strdup, yet you do not free it anywhere, but hopefully that is done somewhere in the unshown code.
The problem here seems to be that there is a copy done for the return of the get sql method which will copy the pointer so now you have got two pointers pointing to the same.
This is a problem because when the first gets destructed, it will also destruct the pointer hopefully in your destructor, and when the second would run, you will be trying to delete a dangling pointer... That is probably what is causing the crash for you.
There are several approaches to address this issue:
- Return reference to avoid the copy.
- Make a proper copy constructor to copy the data, and not the pointer.
- Use higher level C++ elements, like std::string, or at least smart pointers, etc.