Comments (4)
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.
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.
@f-saez No problem! I think when we have enums in the future, it's less likely to be misused.
from mojo.
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)
- [Modular CLI] cannot create account with gmail ? HOT 4
- [BUG] Unexpected constructor resolution due to implicit type casting
- DTypepointer scatter fails to compile inside an object
- [Docs] Install Mojo SSL Error HOT 2
- [BUG] `0! == 0` HOT 5
- [BUG] Wrong type conversion HOT 2
- [Feature Request] Gradual adoption into python project
- [Feature Request] Align Mojo `alias` with `type` HOT 1
- [Feature Request] `--free-standing` option in command line to make embedded systems
- [BUG] mojo test: couldn't build the test executable in current nightlies HOT 3
- [BUG] .mojopkg files do not support nested package directory structures HOT 1
- [BUG] REPL incorrectly displays boolean SIMD vector contents
- [Modular CLI] Running `modular auth` fails with the error message: http error: response code 404 HOT 1
- mojo slower that python HOT 1
- [Modular CLI] modular: error: invalid manifest: expiration has passed HOT 9
- [Feature Request] Inline assembly support HOT 2
- Generalize `init` argument convention with named result slots HOT 1
- [BUG] Strange behavior with conformance
- [BUG] mlir types confuse overload resolution HOT 1
- [BUG] crash when trying to run open3d.visualization.draw(mesh)
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 mojo.