[Lazarus] Debugger stops in c dll even when no breakpoint set

Martin Frb lazarus at mfriebe.de
Wed Nov 24 14:29:51 CET 2021


On 24/11/2021 13:41, Luca Olivetti via lazarus wrote:
> El 3/11/21 a les 15:56, Luca Olivetti via lazarus ha escrit:
>
> A quick follow up: I used the wrong size when mallocing data (size of 
> the pointer variable instead of the size of the struct it pointed to, 
> d'oh!).
> I found it by running the program under windbg and there I saw a 
> message saying that I wrote to a memory area beyond the 4 bytes 
> allocated. I wondered why 4 bytes when my struct is bigger and then I 
> found the stupid error 
> https://github.com/olivluca/fpopen62541/commit/e5ec2cab8e97eeeba29e33b801de6e42a0084ca8
>
> I don't know where that message came from, but is there a way to see 
> it while debugging the application under lazarus?

I would guess this is based on 
https://docs.microsoft.com/de-de/windows-hardware/drivers/debugger/gflags-and-pageheap

Now there are 2 modes: "standard" and "full"
Note: this is only about mem on the heap. Local vars on the stack are 
not affected (except, if (like objects) they are pointers to the heap, 
then the heap part (and only that) will be affected)

"Standard" only detects, overruns when mem is freed.
If I understand this correctly, in FPC you can enable -gh heaptrc, and 
this will do something similar, but limited...
This is hardcoded in HeapTrc
       { add a small footprint at the end of memory blocks, this
         can check for memory overwrites at the end of a block }
       add_tail : boolean = true;
       tail_size : longint = sizeof(ptruint);
Additionally 
https://www.freepascal.org/docs-html/rtl/heaptrc/keepreleased.html

On Linux, you can also use valgrind memcheck => which is excellent at 
finding stuff like this.

"Full" (if I read it correctly) will get a separate mem-page for every 
(m)alloc.
As the doc says, that will strain your sys mem....
This want work with default FPC, because FPC does not use its own mem 
manager. Fpc gets a whole page, and splits it without windows knowing.
So you need to see if there is an alternative mem manager that would 
work (maybe CMEM?).

If there is a mem manager, that works with "FULL", then gdb and fpdebug 
should stop with an access violation.



More information about the lazarus mailing list