@@ -31,12 +31,30 @@ function setupMethods (soljson) {
31
31
} ;
32
32
}
33
33
34
+ var alloc ;
35
+ if ( '_solidity_alloc' in soljson ) {
36
+ alloc = soljson . cwrap ( 'solidity_alloc' , 'number' , [ 'number' ] ) ;
37
+ } else {
38
+ alloc = soljson . _malloc ;
39
+ assert ( alloc , 'Expected malloc to be present.' ) ;
40
+ }
41
+
42
+ var reset ;
43
+ if ( '_solidity_reset' in soljson ) {
44
+ reset = soljson . cwrap ( 'solidity_reset' , null , [ ] ) ;
45
+ }
46
+
34
47
var copyToCString = function ( str , ptr ) {
35
48
var length = soljson . lengthBytesUTF8 ( str ) ;
36
49
// This is allocating memory using solc's allocator.
37
- // Assuming copyToCString is only used in the context of wrapCallback, solc will free these pointers.
38
- // See https://github.com/ethereum/solidity/blob/v0.5.13/libsolc/libsolc.h#L37-L40
39
- var buffer = soljson . _malloc ( length + 1 ) ;
50
+ //
51
+ // Before 0.6.0:
52
+ // Assuming copyToCString is only used in the context of wrapCallback, solc will free these pointers.
53
+ // See https://github.com/ethereum/solidity/blob/v0.5.13/libsolc/libsolc.h#L37-L40
54
+ //
55
+ // After 0.6.0:
56
+ // The duty is on solc-js to free these pointers. We accomplish that by calling `reset` at the end.
57
+ var buffer = alloc ( length + 1 ) ;
40
58
soljson . stringToUTF8 ( str , buffer , length + 1 ) ;
41
59
soljson . setValue ( ptr , buffer , '*' ) ;
42
60
} ;
@@ -136,6 +154,14 @@ function setupMethods (soljson) {
136
154
throw e ;
137
155
}
138
156
removeFunction ( cb ) ;
157
+ if ( reset ) {
158
+ // Explicitly free memory.
159
+ //
160
+ // NOTE: cwrap() of "compile" will copy the returned pointer into a
161
+ // Javascript string and it is not possible to call free() on it.
162
+ // reset() however will clear up all allocations.
163
+ reset ( ) ;
164
+ }
139
165
return output ;
140
166
} ;
141
167
0 commit comments