DEV Community

Discussion on: I'm an Expert in Memory Management & Segfaults, Ask Me Anything!

Collapse
 
codemouse92 profile image
Jason C. McDonald

Yes, this is undefined behavior. The line marked #3 indicates a double-free is occuring somewhere in your code, wherein an already-freed pointer is being freed again. The behavior of a double-free is undefined.

Unfortunately, I don't have enough context here (especially in an image) to debug this for you. It looks like you're already using valgrind. Try compiling your own code with debug flags (-g), and then running it through Valgrind and repeating whatever action triggers this error. The resulting stack trace should point you to the line in your code where the double-free is originating.

Collapse
 
shantanubanerjee75 profile image
Shantanu Banerjee

Thank you very much for your reply.
I am unable to use valgrind on target due to very limited system memory on my embedded device. Could you please suggest any alternate approach which use low resource?
I have compiled my application with -ggdb3.
I have encounter this problem only with wide character text (std::wstring) and during the object destruction (out of scope) this corruption is happening.

Images
dev-to-uploads.s3.amazonaws.com/up...
dev-to-uploads.s3.amazonaws.com/up...
dev-to-uploads.s3.amazonaws.com/up...
dev-to-uploads.s3.amazonaws.com/up...
dev-to-uploads.s3.amazonaws.com/up...
dev-to-uploads.s3.amazonaws.com/up...
dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
shantanubanerjee75 profile image
Shantanu Banerjee

Memory dump of variable from frame #6 of stack trace
dev-to-uploads.s3.amazonaws.com/up...

Could you please provide any hint?

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

If you have memory bugs in your code, it should also work if you compile and test it on your regular machine, even though the architecture is different. Have you tried that?

Thread Thread
 
shantanubanerjee75 profile image
Shantanu Banerjee

Thanks for your advice. I haven't tried to run this on regular pc. This is pretty big task but let me try this. Could you please provide any opinion to use ASAN compiled binary on target?

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

That might work. I haven't worked with ARM boards or microcircuits before, so I don't have much insight there.