Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on IOS : malloc: *** error for object 0x12bc17cc0: pointer being freed was not allocated #127

Open
maksimus1982 opened this issue Dec 27, 2024 · 2 comments

Comments

@maksimus1982
Copy link

maksimus1982 commented Dec 27, 2024

Hello !
I have python class, which reads dafa from DBF file:

from os.path import exists, join, split
import sys
import struct, datetime, decimal, itertools
FILE_ENCODING = 'windows-1251'

class DBF:
@staticmethod
def dbfreader(f, encoding='windows-1251'):
numrec, lenheader = struct.unpack('<xxxxLH22x', f.read(32))
numfields = (lenheader - 33) // 32

    fields = []
    for _ in range(numfields):
        name, typ, size, deci = struct.unpack('<11sc4xBB14x', f.read(32))
        name = name.decode(encoding).replace('\0', '')
        typ = typ.decode(encoding)
        fields.append((name, typ, size, deci))
        print("fields: ",name, typ, size, deci)
    yield [field[0] for field in fields]
    yield [tuple(field[1:]) for field in fields]
    
    terminator = f.read(1)
    assert terminator == b'\r'

    fields.insert(0, ('DeletionFlag', 'C', 1, 0))
    fmt = ''.join(['%ds' % fieldinfo[2] for fieldinfo in fields])
    fmtsiz = struct.calcsize(fmt)

    for _ in range(numrec):
        record = struct.unpack(fmt, f.read(fmtsiz))
        if record[0] != b' ':
            continue
        result = []
        for (name, typ, size, deci), value in zip(fields, record):
            if name == 'DeletionFlag':
                continue
            if typ == "N":
                value = value.decode(encoding).replace('\0', '').lstrip()
                if value == '':
                    value = 0
                elif deci:
                    value = decimal.Decimal(value)
                else:
                    value = int(value)
            elif typ == "C":
                value = value.decode(encoding).rstrip()
            elif typ == 'D':
                y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
                value = datetime.date(y, m, d)
            elif typ == 'L':
                value = (value.decode(encoding) in 'YyTt' and 'T') or \
                        (value.decode(encoding) in 'NnFf' and 'F') or '?'
            result.append(value)
        yield result
       
def __init__(self, filename):
    filename = join( split( sys._getframe().f_code.co_filename )[0], filename )
  
    if not exists( filename ):
        print("filename not found",filename)
        fError( error_DBFfileNE + " '" + filename + "'." )

    try:
        f = open(filename, 'rb')
    except IOError:
        print("filename can open",filename)
        fError( error_DBFfileNO + " '" + filename + "'." )


    try:
        recordList = list(self.dbfreader(f))
        print("Records :",recordList)
    except Exception as e:
        print("Record list failed for file",e)
        fError( error_DBFfileNO + " '" + filename + "'." )

    f.close()

    self.fieldnames, self.fieldspecs, self.records = recordList[0], recordList[1], recordList[2:]
    self.fieldnamesDict = {}
    for name in self.fieldnames:
        self.fieldnamesDict[name] = self.fieldnames.index(name)

def __call__(self):
    for record in self.records:
        record_dict = {}
        for name in self.fieldnamesDict:
            record_dict[name] = record[self.fieldnamesDict[name]]
        yield record_dict

and class usage like this : globals()['db'] = DBF(filename)

On IOS i catch a bug :
// run app
let file = fopen(appPath, "r")
let result = PyRun_SimpleFileEx(file, appPath, 1)
if (result != 0) {
print("Python program completed with error.")
}

Py_Finalize()

flutter: Finished unpacking application archive in 0:00:00.158748
Swift runPython(appPath: /Users/shadmin/Library/Developer/CoreSimulator/Devices/088C3E3D-281F-4AAE-B109-8C030C09390B/data/Containers/Data/Application/ECD9A428-784D-4A62-98A6-C9532A6ACBE8/Library/Application Support/flet/app/_02_111_gear_calc.py, modulePaths: [])
Runner(65895,0x11b409280) malloc: *** error for object 0x12bc17cc0: pointer being freed was not allocated
Runner(65895,0x11b409280) malloc: *** set a breakpoint in malloc_error_break to debug

But on Android all works fine. I can print all DBF content to console for example.

Please help :)

@FeodorFitsner
Copy link
Contributor

Does "hello world" work on iOS simulator for you?

@maksimus1982
Copy link
Author

Yes! Basically everything works on iOS. And I think I found what causes the error.
If i remove "import decimal" and change #value = decimal.Decimal(value) to value = float(value) in DBF python class - everything works fine :)

I don't no why

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

No branches or pull requests

2 participants