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

Program crashes when trying to wrap Miniaudio #10

Open
Keithcat1 opened this issue Sep 14, 2021 · 10 comments
Open

Program crashes when trying to wrap Miniaudio #10

Keithcat1 opened this issue Sep 14, 2021 · 10 comments

Comments

@Keithcat1
Copy link

I tried writing a simple program that uses CPPYY to use Miniaudio from Python. However, at some point it crashes and I have no idea why.
I'm using Windows 10 64-bit, CPython 3.9.5 and here is a ZIP file containg my Python code, my copy of Miniaudio (up-to-date) and miniaudio_dll.dll (to rebuild "cl miniaudio_dll.c")
problem.zip
I don't just JIT compile miniaudio.h because it causes a compilation error. Sorry if I forgot something, but nearly empty laptop batteries wait for no one.

@wlav
Copy link
Owner

wlav commented Sep 14, 2021

I don't currently have access to a Windows box (I've received a loaner Intel macbook to install VMWare, but haven't gotten around to do so yet), so can't run.

However, if you have any more direct description of how/where the code crashes, I might have some idea/guess.

@Keithcat1
Copy link
Author

Keithcat1 commented Sep 14, 2021 via email

@wlav
Copy link
Owner

wlav commented Sep 15, 2021

The cause are these two lines in mini.py:

config.playback.SampleRate = 44100
config.capture.sampleRate = 44100

as neither structure has a sampleRate datamember, only config (top-level) has.

Not 100% sure what is going on yet, but it is clear that the offset returned for SampleRate is the offset in config and then an assignment happens C++-side into first playback, then capture. (cppyy does not use __slots__ for structs but keeps __dict__, so the expected behavior is a harmless, although perhaps puzzling, Python-side insertion in the dicts of playback and capture, not any transfer of data to C).

The code should be (drop the above two lines):

config.sampleRate = 44100

I'll dig a bit further to see what exactly is going on, but the above fix should get you going.

@Keithcat1
Copy link
Author

In Miniaudio, the sampleRate field is defined as:
ma_uint32 sampleRate;
So the name is sampleRate, but why are you telling me to use config.SampleRate instead?
By the way, why aren't slots being used? Wouldn't it raise an exception when you incorrectly modify fields like this? Or do slots make it hard to convert between C and Python?

@wlav
Copy link
Owner

wlav commented Sep 15, 2021

So the name is sampleRate, but why are you telling me to use config.SampleRate instead?

b/c I'm doing half a dozen things in parallel and don't have enough cycles to pay too much attention: I c&p-ed that SampleRate from playback.SampleRate whereas indeed the problematic one is capture.sampleRate.

@wlav
Copy link
Owner

wlav commented Sep 15, 2021

Below is a standalone reproducer that should succeed, but doesn't. Seems to be caused by the structs being anonymous (if they are named, all works as expected),

import cppyy
  
cppyy.cppdef("""\
namespace StructInStruct {

struct Outer {
    Outer() { fO = -77; inner.fI = -99; }

    int fO;

    struct {
        int fI;
    } inner;
};

int check(const Outer& p, int which) {
    if (which == 1) return p.fO;
    if (which == 2) return p.inner.fI;
    return -1;
} }""")

ns = cppyy.gbl.StructInStruct

o = ns.Outer()
assert o.fO == -77
assert ns.check(o, 1) == -77
assert ns.check(o, 2) == -99

o.inner.fO = 42
assert ns.check(o, 1) == -77
assert ns.check(o, 2) == -99

assert hasattr(o.inner, 'fI')

o.inner.fI = 37
assert ns.check(o, 1) == -77
assert ns.check(o, 2) == 37

@Keithcat1
Copy link
Author

Keithcat1 commented Sep 16, 2021 via email

@wlav
Copy link
Owner

wlav commented Sep 17, 2021

Yes, it's clearly not functioning as it should. My best guess at this point is that the anonymous struct is treated the same way as an anonymous enum (which is transparently accessible in the parent class).

@wlav
Copy link
Owner

wlav commented Oct 23, 2021

Fixed in repo.

@wlav
Copy link
Owner

wlav commented Oct 23, 2021

Oops; commenting on wrong bug report. No, this one is NOT yet fixed ...

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