Comments (9)
//
// memory.swift
// pointer-sample
import Foundation
class Memory{
init() {
print("init")
}
deinit {
print("deinit")
}
}
class ShapeDummy{
init() {
print("ShapeDummy init")
}
deinit {
print("ShapeDummy deinit")
}
}
class MemPointer{
var ptr: UnsafeMutableRawBufferPointer
var shapeptr: UnsafeMutableBufferPointer<ShapeDummy>
init(_ array: [Memory], shape: [ShapeDummy]) {
let ptr = create_unsafeMRBPtr(type: Memory.Type.self, count: array.count)
array.withUnsafeBytes{
memcpy(ptr.baseAddress!, $0.baseAddress, ptr.count)
}
let sptr = create_unsafeMBPtrT(type: ShapeDummy.self, count: shape.count)
var shape = shape
self.shapeptr = array2UnsafeMBPtrT(&shape)
self.ptr = ptr
>>>>>>>>>>>>>>>>>>>>>>
self.shapeptr[0] = ShapeDummy() //-> leak!
>>>>>>>>>>>>>>>>>>>>>>
}
deinit {
self.ptr.deallocate()
}
}
//Note that returned value (UnsafeMutableBufferPointer<T>) will not be freed and was not initialized
internal func create_unsafeMBPtrT<T>(type: T.Type, count: Int) -> UnsafeMutableBufferPointer<T>{
typealias pointer = UnsafeMutableBufferPointer<T>
return pointer.allocate(capacity: count)
}
//Note that returned value (UnsafeMutableRawBufferPointer) will not be freed and was not initialized
internal func create_unsafeMRBPtr<T>(type: T.Type, count: Int) -> UnsafeMutableRawBufferPointer{
typealias pointer = UnsafeMutableRawBufferPointer
return pointer.allocate(byteCount: MemoryLayout<T>.stride * count, alignment: MemoryLayout<T>.alignment)
}
internal func array2UnsafeMBPtrT<T>(_ array: inout [T]) -> UnsafeMutableBufferPointer<T>{
let ptr = create_unsafeMBPtrT(type: T.self, count: array.count)
let _ = array.withUnsafeMutableBytes{
memcpy(ptr.baseAddress!, $0.baseAddress!, $0.count)
}
return ptr
}
from matft.
In the first place, I shouldn't set property as pointer...
link
from matft.
class MemPointer{ var ptr: UnsafeMutableRawBufferPointer var shapeptr: UnsafeMutableBufferPointer<ShapeDummy> init(_ array: [Memory], shape: [ShapeDummy]) { let ptr = create_unsafeMRBPtr(type: Memory.Type.self, count: array.count) array.withUnsafeBytes{ memcpy(ptr.baseAddress!, $0.baseAddress, ptr.count) } let sptr = create_unsafeMBPtrT(type: ShapeDummy.self, count: shape.count) var shape = shape self.shapeptr = array2UnsafeMBPtrT(&shape) self.ptr = ptr >>>>>>>>>>>>>>>>>>>>>> self.shapeptr[0] = ShapeDummy() //-> leak! >>>>>>>>>>>>>>>>>>>>>> } deinit { self.ptr.deallocate() } }
prevent memory leak by this
self.shapeptr.deinitialize(count: 3)
I must destroy "stored" data
from matft.
class MemPointer{
var ptr: UnsafeMutableRawPointer
var shapeptr: UnsafeMutablePointer<ShapeDummy>
init(_ array: [Memory], shape: [ShapeDummy]) {
var array = array
self.ptr = array2UnsafeMRPtr(&array)
print(self.ptr.bindMemory(to: Memory.self, capacity: 4)[3].mycnt)
self.ptr.bindMemory(to: Memory.self, capacity: 4)[3] = Memory()
print(self.ptr.bindMemory(to: Memory.self, capacity: 4)[3].mycnt)
//let sptr = create_unsafeMPtrT(type: ShapeDummy.self, count: shape.count)
var shape = shape
self.shapeptr = array2UnsafeMPtrT(&shape)
self.shapeptr.pointee = ShapeDummy()
print(self.shapeptr[1].mycnt)
//self.shapeptr.move()
}
deinit {
print("Mempointer deconstructor called")
self.ptr.bindMemory(to: Memory.self, capacity: 4).deinitialize(count: 4)
self.ptr.deallocate()
self.shapeptr.deinitialize(count: 3)
self.shapeptr.deallocate()
}
}
//Note that returned value (UnsafeMutableBufferPointer<T>) will not be freed and was not initialized
internal func create_unsafeMPtrT<T>(init_value: T, count: Int) -> UnsafeMutablePointer<T>{
typealias pointer = UnsafeMutablePointer<T>
let ptr = pointer.allocate(capacity: count)
ptr.initialize(repeating: init_value, count: count)
return ptr
}
//Note that returned value (UnsafeMutableRawBufferPointer) will not be freed and was not initialized
internal func create_unsafeMRPtr<T>(init_value: T, count: Int) -> UnsafeMutableRawPointer{
typealias pointer = UnsafeMutableRawPointer
let ptr = pointer.allocate(byteCount: MemoryLayout<T>.stride * count, alignment: MemoryLayout<T>.alignment)
ptr.initializeMemory(as: T.self, repeating: init_value, count: count)
return ptr
}
internal func array2UnsafeMPtrT<T>(_ array: inout [T]) -> UnsafeMutablePointer<T>{
let ptr = create_unsafeMPtrT(init_value: array.first!, count: array.count)
let _ = array.withUnsafeMutableBufferPointer{
//memcpy(ptr, $0.baseAddress!, $0.count)
ptr.assign(from: $0.baseAddress!, count: $0.count)
//ptr.moveInitialize(from: $0.baseAddress!, count: $0.count)
}
return ptr
}
internal func array2UnsafeMRPtr<T>(_ array: inout [T]) -> UnsafeMutableRawPointer{
let ptr = create_unsafeMRPtr(init_value: array.first!, count: array.count)
let ret = ptr.bindMemory(to: T.self, capacity: array.count)
let _ = array.withUnsafeMutableBufferPointer{
//memcpy(ptr, $0.baseAddress!, $0.count)
ret.assign(from: $0.baseAddress!, count: $0.count)
//ptr.moveInitialize(from: $0.baseAddress!, count: $0.count)
}
return ptr
}
Log was
Memory0 init
Memory1 init
Memory2 init
Memory3 init
ShapeDummy0 init
ShapeDummy1 init
ShapeDummy2 init
3
Memory4 init
4
ShapeDummy3 init
1
ShapeDummy0 deinit
Memory3 deinit
Mempointer deconstructor called
Memory0 deinit
Memory1 deinit
Memory2 deinit
Memory4 deinit
ShapeDummy3 deinit
ShapeDummy1 deinit
ShapeDummy2 deinit
I must initialize memory first by padding anything?
from matft.
var tmp = [Memory(), Memory(), Memory(), Memory()]
var copptr = array2UnsafeMRPtr(&tmp)
//self.memory.copyMemory(from: copptr, byteCount: MemoryLayout<Memory>.size * 4)// leak!
self.memory.bindMemory(to: Memory.self, capacity: 4).assign(from: copptr.bindMemory(to: Memory.self, capacity: 4), count: 4)//->ok!
copptr.bindMemory(to: Memory.self, capacity: 4).deinitialize(count: 4)
copptr.deallocate()
}
deinit {
print("Mempointer deconstructor called")
self.shapeptr.deinitialize(count: 3)
self.shapeptr.deallocate()
self.memory.assumingMemoryBound(to: Memory.self).deinitialize(count: 4)
self.memory.deallocate()
}
No leak!
from matft.
I can't pursue leak's reason.
I had better use normal array?
from matft.
The reason of problem may be editing shapeptr directly.
I should use "with" function such like
public func withShapeMBPtr<R>(_ body: (UnsafeMutableBufferPointer<Int>) throws -> R) rethrows -> R{
try self.shape.withUnsafeMutableBufferPointer{
try body($0)
}
}
from matft.
let e = Matft.mfarray.arange(start: 0, stop: 27, step: 1, shape: [3,3,3])
print(e[-2~1])
print(e[-4~1])
print(e[-1~1])
print(e[0~4])
print(e[-1~~-1])
print(e[-2~0~-1])
print(e[2~0~-1])
print(e[-1~-8~-1])
print(e[-8~0~-1])
print(e[1,1,1])
e[1,1] = 5
print(e)
let d = Matft.mfarray.conv_order(e[-1~~-1], mforder: .Row)
print(d)
print(e.T)
print(e.T + e[-4~1]) ///->>>>> crash!
from matft.
let d = Matft.mfarray.conv_order(e[-1~~-1], mforder: .Row) print(d) print(e.T) print(e.T + e[-4~1]) ///->>>>> crash!
this one is merely bug.
I think this issue has already resolved.
from matft.
Related Issues (20)
- not found np.unique() HOT 1
- Wrong Shape after "ufuncReduce add" HOT 5
- Matft.math.pow is not working as expected HOT 1
- Add @inline
- Complex support HOT 9
- Refactoring pointer HOT 3
- Adding demo for image processing HOT 3
- Support for atan2 HOT 3
- Feature Request: Numpy Dot HOT 1
- Image processing HOT 5
- Multidimensional MfArray with different length Arrays in axis 1 HOT 2
- Copy On Write implementation HOT 2
- Subscript bug using `Matft.arange()` HOT 4
- Numpy FFT implementation? HOT 11
- Support MLMultiArray HOT 1
- [Bug] Invalid access on some complex operation
- How to contribute - have some small numpy / scipy vDSP implementations HOT 1
- How to get the content of the MfArray? HOT 3
- ToArray doesn't respect slices HOT 2
- Tips to convert an MfArray to MLMultiArray HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matft.