Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Out of memory: cannot allocate 99010677309440-byte block #278

Open
clarkmcc opened this issue Jan 28, 2025 · 1 comment
Open

Out of memory: cannot allocate 99010677309440-byte block #278

clarkmcc opened this issue Jan 28, 2025 · 1 comment

Comments

@clarkmcc
Copy link

I keep crashing my application so I am suspicious that I'm doing something wrong with the darwinkit API, which is translating to some invariant being violated somewhere. Here's the stacktrace:

This seems like it could be related to #277 and just manifesting itself in a different crash.

runtime: out of memory: cannot allocate 99010677309440-byte block (11002378723328 in use)
fatal error: out of memory

goroutine 7418 gp=0x1400100f340 m=12 mp=0x14000600808 [running, locked to thread]:
runtime.throw({0x10605865d?, 0x99cd879?})
	/Users/clarkmccauley/go/go1.23.5/src/runtime/panic.go:1067 +0x38 fp=0xb4197060060 sp=0xb4197060030 pc=0x1049ec548
runtime.(*mcache).allocLarge(0xb4197060128?, 0x5a0cb8300d40, 0x0)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/mcache.go:236 +0x1e4 fp=0xb41970600b0 sp=0xb4197060060 pc=0x10498c174
runtime.mallocgc(0x5a0cb8300d40, 0x106af60e0, 0x1)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/malloc.go:1177 +0x534 fp=0xb4197060150 sp=0xb41970600b0 pc=0x1049e6de4
runtime.newarray(0x1?, 0xb41970601a8?)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/malloc.go:1431 +0x50 fp=0xb4197060180 sp=0xb4197060150 pc=0x1049e73d0
reflect.unsafe_NewArray(0x1f790e220?, 0xb41970601a8?)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/malloc.go:1450 +0x1c fp=0xb41970601a0 sp=0xb4197060180 pc=0x1049e744c
reflect.MakeSlice({0x106bab8e0, 0x10679a760}, 0xb41970601a8, 0xb41970601a8)
	/Users/clarkmccauley/go/go1.23.5/src/reflect/value.go:3204 +0x94 fp=0xb41970601d0 sp=0xb41970601a0 pc=0x104a30814
github.com/progrium/darwinkit/objc.ToGoSlice(0x1049e5c54?, {0x106bab8e0, 0x10679a760})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/type_convertion.go:173 +0xa8 fp=0xb41970602a0 sp=0xb41970601d0 pc=0x105660f78
github.com/progrium/darwinkit/objc.convertToGoValue(0x14004579378, {0x106bab8e0, 0x10679a760})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/type_convertion.go:254 +0x668 fp=0xb41970603f0 sp=0xb41970602a0 pc=0x105662008
github.com/progrium/darwinkit/objc.Call[...]({0x106b6a8a0, 0x600000b35f00}, {0x200e945aa}, {0x0, 0x0, 0x0})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/call.go:66 +0x52c fp=0xb4197060510 sp=0xb41970603f0 pc=0x1059471ac
github.com/inkrprinting/inkragent/pkg/ocr.(*appleVisionEngine).extract.func1.1({{0x0?}}, {{0x0?}})
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/ocr/ocr_engine_apple_darwin.go:28 +0x88 fp=0xb4197060650 sp=0xb4197060510 pc=0x1059451d8
runtime.call16(0x14000f841e0, 0x140004454e0, 0x0, 0x0, 0x0, 0x10, 0xb41970608a0)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/asm_arm64.s:504 +0x78 fp=0xb4197060670 sp=0xb4197060650 pc=0x1049f3398
runtime.reflectcall(0x106af9300?, 0x170d1e5c0?, 0x2?, 0x606527a?, 0x1?, 0x12?, 0x106af9300?)
	<autogenerated>:1 +0x34 fp=0xb41970606b0 sp=0xb4197060670 pc=0x1049f7864
reflect.Value.call({0x106801580?, 0x140004454e0?, 0x0?}, {0x106046a27, 0x4}, {0xb41b595d260, 0x2, 0x10565b010?})
	/Users/clarkmccauley/go/go1.23.5/src/reflect/value.go:581 +0x97c fp=0xb4197060c60 sp=0xb41970606b0 pc=0x104a26d2c
reflect.Value.Call({0x106801580?, 0x140004454e0?, 0x106af9300?}, {0xb41b595d260?, 0x4?, 0x14000e2fe10?})
	/Users/clarkmccauley/go/go1.23.5/src/reflect/value.go:365 +0x94 fp=0xb4197060ce0 sp=0xb4197060c60 pc=0x104a261c4
github.com/progrium/darwinkit/objc.wrapGoFuncAsBlockIMP.func1(0x10856dba0?, 0x170d1e5f0, {0x170d1e450, 0x3, 0x140014fadc8?})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/block.go:202 +0x138 fp=0xb4197060d70 sp=0xb4197060ce0 pc=0x10565b098
github.com/progrium/darwinkit/objc/ffi.handleClosure(0xb41b595c6c0, 0x170d1e5f0, 0x170d1e450, 0x1049ecb28?)
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/ffi/ffi.go:114 +0x9c fp=0xb4197060db0 sp=0xb4197060d70 pc=0x10565635c
_cgoexp_e4f2f4982498_handleClosure(0x140014fae38?)
	_cgo_gotypes.go:231 +0x30 fp=0xb4197060de0 sp=0xb4197060db0 pc=0x105656410
runtime.cgocallbackg1(0x1056563e0, 0x170d1e3f0, 0x0)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/cgocall.go:437 +0x218 fp=0xb4197060eb0 sp=0xb4197060de0 pc=0x10497e0c8
runtime.cgocallbackg(0x1056563e0, 0x170d1e3f0, 0x0)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/cgocall.go:350 +0x10c fp=0xb4197060f20 sp=0xb4197060eb0 pc=0x10497dddc
runtime.cgocallbackg(0x1056563e0, 0x170d1e3f0, 0x0)
	<autogenerated>:1 +0x1c fp=0xb4197060f50 sp=0xb4197060f20 pc=0x1049f75bc
runtime.cgocallback(0xb4197060fc8, 0x105655ac0, 0x1060432ec)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/asm_arm64.s:1131 +0xb0 fp=0xb4197060f80 sp=0xb4197060f50 pc=0x1049f52a0
runtime.systemstack_switch()
	/Users/clarkmccauley/go/go1.23.5/src/runtime/asm_arm64.s:201 +0x8 fp=0xb4197060f90 sp=0xb4197060f80 pc=0x1049f2e38
runtime.cgocall(0x1060432ec, 0xb4197061008)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/cgocall.go:185 +0x70 fp=0xb4197060fd0 sp=0xb4197060f90 pc=0x1049e5c80
github.com/progrium/darwinkit/objc/ffi._Cfunc_ffi_call0(0xb41b595c7e0, 0x1a27a9028, 0x140045791f0, 0x14000445540)
	_cgo_gotypes.go:153 +0x30 fp=0xb4197061000 sp=0xb4197060fd0 pc=0x105655ac0
github.com/progrium/darwinkit/objc/ffi.Call.func1(0xb41b595c7e0?, 0x1a27a9028, 0x19ebfe330?, {0x14000445540?, 0x200000000?, 0x1f6823d80?})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/ffi/ffi.go:75 +0x70 fp=0xb4197061050 sp=0xb4197061000 pc=0x105655f90
github.com/progrium/darwinkit/objc/ffi.Call(0xb41b595c7e0, 0x14000445560?, 0x140045791f0, {0x14000445540, 0x4, 0x4})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/ffi/ffi.go:75 +0x30 fp=0xb41970610b0 sp=0xb4197061050 pc=0x105655ec0
github.com/progrium/darwinkit/objc.Call[...]({0x106b703a0, 0x600002ba1260}, {0x2011e4e7b}, {0x140014fb1e8, 0x2, 0x1400100f340})
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/call.go:61 +0x4a0 fp=0xb41970611a0 sp=0xb41970610b0 pc=0x105668f20
github.com/progrium/darwinkit/macos/vision.ImageRequestHandler.PerformRequestsError({{0x140014fb278?}}, {0xb4197068f10, 0x1, 0x1}, 0x0)
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/macos/vision/image_request_handler.gen.go:235 +0xbc fp=0xb4197061220 sp=0xb41970611a0 pc=0x105821adc
github.com/inkrprinting/inkragent/pkg/ocr.(*appleVisionEngine).extract.func1()
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/ocr/ocr_engine_apple_darwin.go:56 +0x180 fp=0xb4197061280 sp=0xb4197061220 pc=0x105945130
github.com/progrium/darwinkit/objc.runTaskAndDeleteHandle(0x63)
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/runtime_utils.go:52 +0x58 fp=0xb41970612c0 sp=0xb4197061280 pc=0x10565fff8
_cgoexp_18cc80d1317f_runTaskAndDeleteHandle(0x140014fb338?)
	_cgo_gotypes.go:1252 +0x20 fp=0xb41970612e0 sp=0xb41970612c0 pc=0x105664970
runtime.cgocallbackg1(0x105664950, 0x170d1eeb8, 0x0)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/cgocall.go:437 +0x218 fp=0xb41970613b0 sp=0xb41970612e0 pc=0x10497e0c8
runtime.cgocallbackg(0x105664950, 0x170d1eeb8, 0x0)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/cgocall.go:350 +0x10c fp=0xb4197061420 sp=0xb41970613b0 pc=0x10497dddc
runtime.cgocallbackg(0x105664950, 0x170d1eeb8, 0x0)
	<autogenerated>:1 +0x1c fp=0xb4197061450 sp=0xb4197061420 pc=0x1049f75bc
runtime.cgocallback(0xb41970614c8, 0x105658a70, 0x1060421b4)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/asm_arm64.s:1131 +0xb0 fp=0xb4197061480 sp=0xb4197061450 pc=0x1049f52a0
runtime.systemstack_switch()
	/Users/clarkmccauley/go/go1.23.5/src/runtime/asm_arm64.s:201 +0x8 fp=0xb4197061490 sp=0xb4197061480 pc=0x1049f2e38
runtime.cgocall(0x1060421b4, 0xb4197061508)
	/Users/clarkmccauley/go/go1.23.5/src/runtime/cgocall.go:185 +0x70 fp=0xb41970614d0 sp=0xb4197061490 pc=0x1049e5c80
github.com/progrium/darwinkit/objc._Cfunc_Run_WithAutoreleasePool(0x63)
	_cgo_gotypes.go:999 +0x30 fp=0xb4197061500 sp=0xb41970614d0 pc=0x105658a70
github.com/progrium/darwinkit/objc.WithAutoreleasePool(0xc0?)
	/Users/clarkmccauley/go/pkg/mod/github.com/progrium/[email protected]/objc/runtime_utils.go:35 +0x30 fp=0xb4197061520 sp=0xb4197061500 pc=0x10565ff80
github.com/inkrprinting/inkragent/pkg/ocr.(*appleVisionEngine).extract(0x140014fb5c8?, {0x1060712a6?, 0x140014fb5e8?}, 0xb41b595c5d0, 0xb4197068e40)
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/ocr/ocr_engine_apple_darwin.go:18 +0xd4 fp=0xb41970615a0 sp=0xb4197061520 pc=0x105944ee4
github.com/inkrprinting/inkragent/pkg/ocr.(*engineDispatcher).extract(0x140007b5b80, {0x106b8be30, 0xb41b595c510}, 0x9c?, 0xb41b595c5d0, 0xb4197068e40)
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/ocr/ocr_engine.go:60 +0x328 fp=0xb41970616c0 sp=0xb41970615a0 pc=0x105944dc8
github.com/inkrprinting/inkragent/pkg/ocr.(*Manager).TextFromImage(0x14000c28460, {0x106b8be30, 0xb41b595c510}, 0xb41b595c5d0)
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/ocr/manager.go:109 +0x84 fp=0xb4197061700 sp=0xb41970616c0 pc=0x105944424
github.com/inkrprinting/inkragent/pkg/hubserver.(*Server).OCRExtraction(0x140007cdea0, {0x106b8be30, 0xb41b595c510}, 0xb419705aa50)
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/hubserver/server_ocr.go:27 +0x254 fp=0xb4197061860 sp=0xb4197061700 pc=0x105e190e4
github.com/inkrprinting/inkragent/pkg/hubserver._Hub_OCRExtraction_Handler({0x106a3e840, 0x140007cdea0}, {0x106b8be30, 0xb41b595c510}, 0x14000c8d900, 0x0)
	/Users/clarkmccauley/Documents/software-development/inkr/repositories/inkragent/pkg/hubserver/hubserver_grpc.pb.go:121 +0x1c0 fp=0xb41970618b0 sp=0xb4197061860 pc=0x105e14a80
google.golang.org/grpc.(*Server).processUnaryRPC(0x14000a36600, {0x106b8be30, 0xb41b595c4b0}, 0x14000d97c80, 0x14000d32000, 0x107d3ddc0, 0x0)
	/Users/clarkmccauley/go/pkg/mod/google.golang.org/[email protected]/server.go:1392 +0xc44 fp=0xb4197061d80 sp=0xb41970618b0 pc=0x104ed5874
google.golang.org/grpc.(*Server).handleStream(0x14000a36600, {0x106b8c888, 0x140009a5860}, 0x14000d97c80)
	/Users/clarkmccauley/go/pkg/mod/google.golang.org/[email protected]/server.go:1802 +0x910 fp=0xb4197061f60 sp=0xb4197061d80 pc=0x104ed9ad0
google.golang.org/grpc.(*Server).serveStreams.func2.1()
	/Users/clarkmccauley/go/pkg/mod/google.golang.org/[email protected]/server.go:1030 +0x84 fp=0xb4197061fd0 sp=0xb4197061f60 pc=0x104ed38f4
runtime.goexit({})
	/Users/clarkmccauley/go/go1.23.5/src/runtime/asm_arm64.s:1223 +0x4 fp=0xb4197061fd0 sp=0xb4197061fd0 pc=0x1049f5394
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 7195
	/Users/clarkmccauley/go/pkg/mod/google.golang.org/[email protected]/server.go:1041 +0x13c

And here's the ocr_engine_apple_darwin.go file

package ocr

import (
	"context"
	"errors"
	"github.com/progrium/darwinkit/macos/foundation"
	"github.com/progrium/darwinkit/macos/vision"
	"github.com/progrium/darwinkit/objc"
)

var _ engine = (*appleVisionEngine)(nil)

type appleVisionEngine struct{}

func (a *appleVisionEngine) extract(_ context.Context, image *Image, onProgress func(int32)) (out []ExtractedLines, err error) {
	defer onProgress(100)

	objc.WithAutoreleasePool(func() {
		handler := vision.NewImageRequestHandler().InitWithDataOptions(image.Bytes, nil)

		req := vision.NewRecognizeTextRequest().InitWithCompletionHandler(func(request vision.Request, error foundation.Error) {
			if !error.IsNil() {
				err = errors.New(error.Description())
				return
			}

			// Get results and ensure they are handled correctly
			results := objc.Call[[]vision.RecognizedTextObservation](request, objc.Sel("results"))

			// Iterate over results safely
			for _, result := range results {
				boundingBox := result.BoundingBox()

				// Convert Vision's bottom-left origin to top-left origin
				x0 := int(boundingBox.Origin.X * float64(image.Width))
				y0 := int((1.0 - (boundingBox.Origin.Y + boundingBox.Size.Height)) * float64(image.Height)) // Flip Y-axis
				x1 := int((boundingBox.Origin.X + boundingBox.Size.Width) * float64(image.Width))
				y1 := int((1.0 - boundingBox.Origin.Y) * float64(image.Height)) // Flip Y-axis

				// Get the recognized text
				for _, candidate := range result.TopCandidates(1) {
					out = append(out, ExtractedLines{
						Bounding: [4]int{x0, y0, x1, y1},
						Line:     candidate.String(),
					})
				}
			}
		})

		req.SetMinimumTextHeight(1 / 100)
		req.SetRecognitionLevel(vision.RequestTextRecognitionLevelAccurate)
		req.SetRecognitionLanguages([]string{"en"})
		//req.SetUsesLanguageCorrection(true)
		//req.SetCustomWords([]string{"copy", "print", "waiting", "black", "white", "yellow", "magenta", "cyan", "light"})

		handler.PerformRequestsError([]vision.IRequest{req}, nil)
	})

	return
}

func newAppleVisionEngine() *appleVisionEngine {
	return &appleVisionEngine{}
}
@clarkmcc
Copy link
Author

clarkmcc commented Jan 28, 2025

Looks like the problem is results isn't always the same type. When results contains at least one result, then

objc.Call[[]vision.RecognizedTextObservation](request, objc.Sel("results"))

...works fine. But if there are zero results, then we get this crash. I found that this was because with one or more results, the returned class is __NSFrozenArrayM, but with zero results, it's __NSArray0.

Doing this check seemed to solve the problem

obj := objc.Call[objc.Object](request, objc.Sel("results"))
count := objc.Call[uint64](obj, objc.Sel("count"))
if count == 0 {
	return // empty results
}
results := objc.Call[[]vision.RecognizedTextObservation](request, objc.Sel("results"))

This feels like the kind of thing that should be handled by these bindings. If not, this is a pretty big footgun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant