Coder Social home page Coder Social logo

[BUG] trouble with Optional about mojo HOT 4 CLOSED

f-saez avatar f-saez commented on August 19, 2024
[BUG] trouble with Optional

from mojo.

Comments (4)

soraros avatar soraros commented on August 19, 2024 1

Why do you use DTypePointer[DType.uint8, 1] instead of DTypePointer[DType.uint8]? Note that the 1 is address_space and not SIMD width.

Your ffi.DLHandle example is likely a different issue, could you open a separate issue for that?

from mojo.

f-saez avatar f-saez commented on August 19, 2024 1

thanks.

I should have use

DTypePointer[DType.uint8,AddressSpace.GENERIC]

and I don't understand why I haven't see that.

I feel so stupid :-(

Apologies.

from mojo.

soraros avatar soraros commented on August 19, 2024 1

@f-saez No problem! I think when we have enums in the future, it's less likely to be misused.

from mojo.

f-saez avatar f-saez commented on August 19, 2024

may it's related, maybe not, but let's try some other code very similiar to the former one.

It may not work on Mac or Windows.

The aim is to link dynamically to a library and call a function to create a context, then destroy this context in the destructor.

from collections import Optional
import sys.ffi

alias LIBLCMS2_NAME = "liblcms2.so"
alias cmsCreateContext = fn(UnsafePointer[UInt8], UnsafePointer[UInt8]) -> UnsafePointer[UInt8]
alias cmsDeleteContext = fn(UnsafePointer[UInt8]) -> Bool  # return void in real life

@value
struct MyStruct:
    var _handle          : UnsafePointer[UInt8]
    var _liblcms2_handle : ffi.DLHandle

    fn __init__(inout self, handle : UnsafePointer[UInt8], liblcms2_handle : ffi.DLHandle):
        self._handle = handle
        self._liblcms2_handle = liblcms2_handle

    @staticmethod
    fn new() -> Optional[Self]:
        var result = Optional[Self](None)
        var liblcms2_handle = ffi.DLHandle(LIBLCMS2_NAME, ffi.RTLD.NOW)   
        if liblcms2_handle.__bool__():
            var handle = liblcms2_handle.get_function[cmsCreateContext]("cmsCreateContext")(UnsafePointer[UInt8](), UnsafePointer[UInt8]())
            result = Optional[Self](Self(handle, liblcms2_handle))
        return result

    fn print(self):
        print("something")

    fn __del__(owned self):
        print("destructor")
        _ =  self._liblcms2_handle.get_function[cmsDeleteContext]("cmsDeleteContext")(self._handle)
        self._liblcms2_handle.close()


fn main():
    var x = MyStruct.new()    
    if x:
        var y = x.value()[]
        y.print()

I got this

destructor
something
destructor
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0.      Program arguments: mojo test.mojo
 #0 0x0000560f20985438 (/home/frank/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x12a1438)
 #1 0x0000560f2098325e (/home/frank/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x129f25e)
 #2 0x0000560f20985acd (/home/frank/.modular/pkg/packages.modular.com_mojo/bin/mojo+0x12a1acd)
 #3 0x00007fcd5b6c5710 __restore_rt (/lib64/libc.so.6+0x40710)
 #4 0x00007fcd5bc8daed do_lookup_x /usr/src/debug/glibc-2.39-15.fc40.x86_64/elf/dl-lookup.c:404:48
 #5 0x00007fcd5bc8e76e _dl_lookup_symbol_x /usr/src/debug/glibc-2.39-15.fc40.x86_64/elf/dl-lookup.c:792:9
 #6 0x00007fcd5b7eaba5 do_sym (/lib64/libc.so.6+0x165ba5)
 #7 0x00007fcd5b716f30 dlsym_doit (/lib64/libc.so.6+0x91f30)
 #8 0x00007fcd5bc85523 set_catch /usr/src/debug/glibc-2.39-15.fc40.x86_64/elf/dl-catch.c:69:6
 #9 0x00007fcd5bc85523 _dl_catch_exception /usr/src/debug/glibc-2.39-15.fc40.x86_64/elf/dl-catch.c:238:7
#10 0x00007fcd5bc85679 _dl_catch_error /usr/src/debug/glibc-2.39-15.fc40.x86_64/elf/dl-catch.c:257:12
#11 0x00007fcd5b716913 _dlerror_run (/lib64/libc.so.6+0x91913)

The destructor is called two times, probably one for x when x get out of scope, and one for y, but aren't they supposed to be the same object ?

Anyway, the first time, the destructor do what it is supposed to do, but the second time, obviously, it crashes in libc because it has already release the context the first time. A Double free I guess.
On more complex code, it could mean random crashes before y goes out of scope.

Obviously, if I change

    fn __del__(owned self):
        print("destructor")
        _ =  self._liblcms2_handle.get_function[cmsDeleteContext]("cmsDeleteContext")(self._handle)
        self._liblcms2_handle.close()

to

    fn close(inout self):
        _ =  self._liblcms2_handle.get_function[cmsDeleteContext]("cmsDeleteContext")(self._handle)
        self._liblcms2_handle.close()

    fn __del__(owned self):
        print("destructor")

I can call manually close() and everything's fine because the destructor is empty :-)

from mojo.

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.