Skip to content

Commit 303abd9

Browse files
authored
fix: use signer's own Free() instead of system libc free on Windows (#135)
* fix: use signer's own Free() instead of system libc free on Windows
1 parent 9c304f5 commit 303abd9

10 files changed

Lines changed: 11 additions & 22 deletions

lighter/libc.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

lighter/signer_client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from lighter.models.resp_send_tx import RespSendTx
2121
from lighter.models.resp_send_tx_batch import RespSendTxBatch
2222
from lighter.transactions import CreateOrder, CancelOrder, Withdraw, CreateGroupedOrders
23-
from lighter.libc import free
2423

2524
CODE_OK = 200
2625

@@ -99,8 +98,10 @@ def decode_and_free(ptr: Any) -> Optional[str]:
9998
return c_str.decode('utf-8')
10099
return None
101100
finally:
102-
# Free the memory allocated by the C library
103-
free(ptr)
101+
# Free the memory using the signer's own Free function to ensure
102+
# the same C runtime that allocated the memory also frees it.
103+
# This is critical on Windows where different CRTs have separate heaps.
104+
__signer.Free(ptr)
104105

105106

106107
def __populate_shared_library_functions(signer):
@@ -174,6 +175,9 @@ def __populate_shared_library_functions(signer):
174175
signer.SignApproveIntegrator.argtypes = [ctypes.c_longlong, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_int, ctypes.c_longlong]
175176
signer.SignApproveIntegrator.restype = SignedTxResponse
176177

178+
signer.Free.argtypes = [ctypes.c_void_p]
179+
signer.Free.restype = None
180+
177181

178182
def get_signer():
179183
# check if singleton exists already
272 Bytes
Binary file not shown.

lighter/signers/lighter-signer-darwin-arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ extern SignedTxResponse SignUpdateMargin(int cMarketIndex, long long cUSDCAmount
144144
extern SignedTxResponse SignStakeAssets(long long cStakingPoolIndex, long long cShareAmount, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
145145
extern SignedTxResponse SignUnstakeAssets(long long cStakingPoolIndex, long long cShareAmount, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
146146
extern SignedTxResponse SignApproveIntegrator(long long cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long cApprovalExpiry, long long cNonce, int cApiKeyIndex, long long cAccountIndex);
147+
extern void Free(void* ptr);
147148

148149
#ifdef __cplusplus
149150
}

lighter/signers/lighter-signer-linux-amd64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ extern SignedTxResponse SignUpdateMargin(int cMarketIndex, long long int cUSDCAm
136136
extern SignedTxResponse SignStakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137137
extern SignedTxResponse SignUnstakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
138138
extern SignedTxResponse SignApproveIntegrator(long long int cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long int cApprovalExpiry, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
139+
extern void Free(void* ptr);
139140

140141
#ifdef __cplusplus
141142
}
808 Bytes
Binary file not shown.

lighter/signers/lighter-signer-linux-arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ extern SignedTxResponse SignUpdateMargin(int cMarketIndex, long long int cUSDCAm
136136
extern SignedTxResponse SignStakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137137
extern SignedTxResponse SignUnstakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
138138
extern SignedTxResponse SignApproveIntegrator(long long int cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long int cApprovalExpiry, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
139+
extern void Free(void* ptr);
139140

140141
#ifdef __cplusplus
141142
}
1.02 KB
Binary file not shown.
3.54 KB
Binary file not shown.

lighter/signers/lighter-signer-windows-amd64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ extern __declspec(dllexport) SignedTxResponse SignUpdateMargin(int cMarketIndex,
136136
extern __declspec(dllexport) SignedTxResponse SignStakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
137137
extern __declspec(dllexport) SignedTxResponse SignUnstakeAssets(long long int cStakingPoolIndex, long long int cShareAmount, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
138138
extern __declspec(dllexport) SignedTxResponse SignApproveIntegrator(long long int cIntegratorIndex, uint32_t cMaxPerpsTakerFee, uint32_t cMaxPerpsMakerFee, uint32_t cMaxSpotTakerFee, uint32_t cMaxSpotMakerFee, long long int cApprovalExpiry, long long int cNonce, int cApiKeyIndex, long long int cAccountIndex);
139+
extern __declspec(dllexport) void Free(void* ptr);
139140

140141
#ifdef __cplusplus
141142
}

0 commit comments

Comments
 (0)