Giter Site home page Giter Site logo

memory leak... about matft HOT 9 CLOSED

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
memory leak...

from matft.

Comments (9)

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
//
//  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.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024

In the first place, I shouldn't set property as pointer...
link

from matft.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
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.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
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.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
    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.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024

I can't pursue leak's reason.
I had better use normal array?

from matft.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024

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.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
        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.

jjjkkkjjj avatar jjjkkkjjj commented on May 25, 2024
        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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.