Skip to content

[macOS M1] sfree triggers "pointer being freed was not allocated" #30

@denguir

Description

@denguir

Minimal example

Hello, I came across a strange behaviour of libcsptr when run on macOS M1 vs Ubuntu.

  • macOS M1 + Homebrew libcsptr 2.0.4 → crash
  • Ubuntu (PPA) libcsptr 2.0.5-7 → works fine
#include <stdio.h>
#include <csptr/smart_ptr.h>

int main(void) {
    int *a = smalloc(.size = sizeof(int), .nmemb = 3, .kind = SHARED);
    int *view = sref(a);

    sfree(view); // dec refcount, should NOT free yet
    sfree(a);    // last owner, SHOULD free now

    printf("ok\n");
    return 0;
}

macOS M1 + Homebrew libcsptr 2.0.4

Compilation

cc -std=c99 -O0 -g main.c \                             
  -I"$(brew --prefix libcsptr)/include" \
  -L"$(brew --prefix libcsptr)/lib" -lcsptr \
  -o main

Output

leaks --atExit -- ./main
main(27181) MallocStackLogging: could not tag MSL-related memory as no_footprint, so those pages will be included in process footprint - (null)
main(27181) MallocStackLogging: recording malloc (and VM allocation) stacks using lite mode
main(27181,0x1f08620c0) malloc: *** error for object 0x12ff04118: pointer being freed was not allocated
main(27181,0x1f08620c0) malloc: *** set a breakpoint in malloc_error_break to debug
Process 27181 is not debuggable. Due to security restrictions, leaks can only show or save contents of readonly memory of restricted processes.

Process:         main [27181]
Path:            /Users/USER/*/main
Load Address:    0x104af4000
Identifier:      main
Version:         0
Code Type:       ARM64
Platform:        macOS
Parent Process:  leaks [27180]
Target Type:     live task

Date/Time:       2025-08-31 13:02:49.387 +0200
Launch Time:     2025-08-31 13:02:48.782 +0200
OS Version:      macOS 15.6 (24G84)
Report Version:  7
Analysis Tool:   /usr/bin/leaks

Physical footprint:         2305K
Physical footprint (peak):  2321K
Idle exit:                  untracked
----

leaks Report Version: 4.0, multi-line stacks
Process 27181: 186 nodes malloced for 14 KB
Process 27181: 0 leaks for 0 total leaked bytes.

ok%                                 

Ubuntu (PPA) libcsptr 2.0.5-7

Compilation

gcc -std=c99 -O0 -g main.c -lcsptr -o main

Output

valgrind ./main
==30520== Memcheck, a memory error detector
==30520== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==30520== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==30520== Command: ./main
==30520== 
ok==30520== 
==30520== HEAP SUMMARY:
==30520==     in use at exit: 0 bytes in 0 blocks
==30520==   total heap usage: 2 allocs, 2 frees, 1,096 bytes allocated
==30520== 
==30520== All heap blocks were freed -- no leaks are possible
==30520== 
==30520== For lists of detected and suppressed errors, rerun with: -s
==30520== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions