开发者

C++ delete pointer with valid address

开发者 https://www.devze.com 2023-03-26 22:38 出处:网络
I am using Poco C++ library and cause strange problem. Poco using own shared pointer class SharedPtr for internal pointer operations. At my case static object Poco::SSLManager has SharedPtr members of

I am using Poco C++ library and cause strange problem. Poco using own shared pointer class SharedPtr for internal pointer operations. At my case static object Poco::SSLManager has SharedPtr members of certificate Handlers objects. When program run ends, static object is deleted and I catch segmentation fault. Uses GDB debugger i see core dump and don't understand problem. Seg faul开发者_开发知识库t is occurred when deleting SharedPtr object (simple operation: delete pObj), but object has valid address, such as - 0x8fcbed8.

Why delete pointer with valid address can cause segmentation fault?

It's may be because object create in fork copy of application and destroy in main?


A valid address is just an accessible address. That doesn't mean that it's right for delete. You can only delete what you got back from new. If you didn't new it, you can't delete it. Deleting a static or automatic object is undefined behaviour- as well as one you might obtain from any other source apart from new.


You speak about using a shared pointer. If that delete is the last delete on the pObj, it might be that it's the last time that the object is being deleted.. and so the object that the shared pointer points to will finally be destroyed. In that case the destructor of pObj is called and maybe you get the Seg Fault from there.

On the other hand depending on which implementation of shared pointer the Poco lib uses, it might be that you are misusing the shared pointer by calling delete on it.


as the other have said, it's not because your pointer looks "good" like 0x8fcbed8 that it's a correct pointer.

In fact, if you use "delete", the pointer will keeps its value. But you should not use it any more. (It's a good practice to set it to NULL just after a delete so it appears "empty" with a debugger.)

There is a tool that could help you to find what is wrong if you are developping with linux. It's valgrind :

valgrind  your_program  [args]

(just add "valgrind" in front of the command you usually launch. Install valgrind if not already here, must have a packet for it on your distro since it is a widely used tool.)

Then valgrind will inspect your program while it run (slowing it a bit) and report you immediatly as soon as you delete something you should not. (and a lot of other errors)


It's very difficult problem and i don't understand problem full, but i try to explain it and my resolution. Problem is platform-specific and occurres only on Gentoo linux with gcc 4.5.5 compiler with Poco Libraries.

I have daemon that use modules (plugins) for processing different requests. Modules and daemon use one static library that use poco library. In static library have code to create SSL connections and as result creates SSL manager. Static library is linked to module and to deamon.

And after end's of working deamon i have segmentation fault that describes in my question. I change static library to shared library and problem is gone, no seg faults. All works fine.

Really, i don't understant it, but seems it's gcc compiler bug on gentoo. On others linux with other gcc all works good with static library.

0

精彩评论

暂无评论...
验证码 换一张
取 消