'Out of Memory Error When Calling DLL function
I'm trying to send an email to someone using python and Nim. I want for the script to work with a DLL, so I compiled my Nim code into a DLL. When loading the DLL into python and calling a function from it, it displays this error:
out of memory
Process finished with exit code 1
The function call and DLL loading:
import ctypes
email_ = ctypes.CDLL("email_.dll", winmode=0)
message = email_.message
message.argtypes = [ctypes.c_wchar_p]
message.restype = None
message(
'Subj',
'msg',
'[email protected]',
'frompassword',
'[email protected]'
)
And Nim DLL source code:
import smtp
import strutils
proc message*(
subject: string,
body: string,
mfrom: string,
password: string,
mto: string
) {.cdecl, exportc, dynlib.} =
var msg = smtp.createMessage(
subject,
body,
@[mto]
)
let smtpConn = newSmtp(useSsl=true, debug=true)
smtpConn.connect("smtp.gmail.com", Port(465))
smtpConn.auth(mfrom.split('@')[0], password)
smtpConn.sendMail(mfrom, @[mto], $msg)
What is the cause of this error and how can I fix it?
Solution 1:[1]
The Nim documentation on interfacing with different languages mentions that before calling Nim code one has to call NimMain(). Presumably this could be necessary for Nim's garbage collection code to work, and maybe the out of error you are getting is nim code trying to reserve memory but the garbage collector not being ready yet and refusing to cooperate.
There could be other issues with such raw ctypes invokation, so I would recommend looking at Nim's package directory regarding python libraries. For instance, nimpy seems to be able to easily export Nim code to Python, and hopefully provides all the necessary plumbing that you may otherwise lack with ctypes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Grzegorz Adam Hankiewicz |
