Skip to content

Comments

Allow the native pointer to be stolen#16

Merged
neuecc merged 2 commits intoCysharp:masterfrom
Xpl0itR:stealable-pointer
Jan 6, 2025
Merged

Allow the native pointer to be stolen#16
neuecc merged 2 commits intoCysharp:masterfrom
Xpl0itR:stealable-pointer

Conversation

@Xpl0itR
Copy link
Contributor

@Xpl0itR Xpl0itR commented Dec 30, 2024

That is, the NativeMemoryArray is no longer the "owner" of the pointer and it is now the responsibility of the caller to free the memory.

Real life example:

public unsafe int LoadFromBuffer(NativeMemoryArray<byte> buffer, string schema = "main") =>
    SQLitePCL.raw.sqlite3_deserialize(
        SqliteConnection.Handle,
        schema,
        (nint)buffer.StealPointer(),
        buffer.Length,
        buffer.Length,
        raw.SQLITE_DESERIALIZE_FREEONCLOSE | raw.SQLITE_DESERIALIZE_RESIZEABLE);

That is, the NativeMemoryArray is no longer the "owner" of the pointer and it is now the responsibility of the caller to free the memory.
@Xpl0itR
Copy link
Contributor Author

Xpl0itR commented Dec 30, 2024

If we would prefer to not add another field, we could replace isDisposed and isStolen with something like canDispose.

@neuecc
Copy link
Member

neuecc commented Jan 6, 2025

Thank you, I think it's good.
I'll release it!

@neuecc neuecc merged commit f5f0db1 into Cysharp:master Jan 6, 2025
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

Successfully merging this pull request may close these issues.

2 participants