|
haha
Real programmers use butterflies
|
|
|
|
|
Update: I killed it. It took me the better part of the day to get it right, and then get advanced with how i was using it, and then integrate that into the projects that go with the library but it's done. Woo. Changed the way I was doing interop somewhat, and it's more complicated now but it still works - although perf could be slightly improved (does two HGlobal allocs where it could in theory use one)
I sometimes really can't stand managed code.
I've got a MIDI callback I'm using while streaming playback, and it's supposed to notify me when the stream needs more data. It forwards that callback to an event off of my MidiStream object
Well, it does that fine, and in the console app i wrote it has no problem feeding more data to the stream each time.
I had been working on it all night, and finally thought I had it licked.
In a windows forms app on the the other hand, it works for awhile, and then randomly crashes because for some reason the delegate that handles the callback has been garbage collected. I have no idea why. It never should have been and doesn't appear to go out of scope anywhere.
Worse, I can't even catch the crash in the debugger - it just exits entirely without so much as an error message
I'm at a loss, and I can't release with this stability issue.
I miss C and C++ right about now.
Real programmers use butterflies
modified 27-Jun-20 22:25pm.
|
|
|
|
|
I think a very known situation for which google gives a lot of hints
Btw: Is also PInvoke involved?
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Yeah it is, but i'm pretty sure my p/invoke calls are correct
Real programmers use butterflies
|
|
|
|
|
You are supposed to "pin" the delegate so that the GC doesn't move it around.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Yep
It does not solve my Problem, but it answers my question
modified 19-Jan-21 21:04pm.
|
|
|
|
|
No, though that works. The delegate object being moved by the GC doesn't matter, that is handled correctly. But you must prevent it from being collected, and pinning has that as a side effect.
|
|
|
|
|
you know what?
I thought I had taken care of that but you just reminded me of a spot where I missed it.
I use StructureToPtr in what area to package the return as a handle instead of a struct by ref (which is what i need) that way I can pass it around without it being copied.
I suppose I could rewrite that as a class, but then i have to pass around the actual structure and break encapsulation.
Either way, thank you. I think you just led me to the problem.
Real programmers use butterflies
|
|
|
|
|
I hope you weren't too far along in porting the whole thing to C++.
|
|
|
|
|
hah, nah and I got it working. I still have a memory leak but it should go away when I refactor
Real programmers use butterflies
|
|
|
|
|
So the error is "A callback was made on a garbage collected delegate of type [whatever]", right?
honey the codewitch wrote: It never should have been and doesn't appear to go out of scope anywhere. Well who knows. The GC thinks it's dead, and effectively it's the Source of Truth even when it's wrong.
Does it go away if you intentionally leak the delegate? (stick it in a static list)
|
|
|
|
|
|
it didn't help but another commenter reminded me of something i did that could have caused it. I have to retool some code
Real programmers use butterflies
|
|
|
|
|
I have the same-ish problem!
It's not garbage collected, but disposed.
I'm reading some files and then process them.
The processing takes a while, around 45 minutes.
For the first 30 minutes everything goes well, but after 30 minutes I suddenly get an ObjectDisposedException from my Entity Framework Core context
I'm not disposing it and if I did, I'd expect it to blow well before 30 minutes since it's a loop.
I'm not aware of any automatic disposes, but 30 minutes sounds like some timeout setting.
While writing this I'm thinking of something that could be the problem...
|
|
|
|
|
Ouch! Good luck!
Real programmers use butterflies
|
|
|
|
|
Sander Rossel wrote: While writing this I'm thinking of something that could be the problem... Isn't your rubber duck listening today?
|
|
|
|
|
A friend once asked the same, but made a typo.
He ended up asked about my rubber d... Well, just check the letter next to "U"
Since then I can't read or hear "rubber duck" without thinking about it
|
|
|
|
|
If that's what you do to your rubber duck, no wonder it isn't listening.
|
|
|
|
|
We had similar(-ish, but the opposite way round) problems with the JVM's GC, while interfacing it to a COM component we needed to use. Instead of releasing objects before we wanted, the GC was holding onto COM reference wrapper objects long after their lifetime was over (because the objects were small, so the GC wasn't experiencing any memory pressure). This meant that the actual COM objects weren't being released, so were exhausting memory outside of the JVM :-/
GC works fine for memory allocated and used solely within the containing virtual machine environment - anything slightly more complex, and you're dicing with death (of your hopes and dreams).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I've run into the same problem as you before. The solution is to marshal the objects as IntPtrs, and manually release them when you're done, if I recall correctly. That way you control the lifetime explicitly. It makes things easier.
I solved the problem with my MIDI code in the OP, and then improved on it several times to make it more efficient and easier to read the code. Now in my entire streaming codebase i only have one win32 global heap allocation that happens once you open a device, until you close it. I've taken to marshalling all structs as ref (struct) except for a very complicated vector of variable length structs which I have to mangle manually.
So it's golden now, and even mostly easier to read and maintain when I'm done. Cool beans.
Real programmers use butterflies
|
|
|
|
|
I did it slightly differently - I wrote a class to implement something like an Objective-C autorelease pool, which our COM object wrappers added themselves to automatically. The pool was released in a finally block & that would release all the COM objects that had been allocated and finished - something like this...
AutoReleasePool.createPool();
try {
... Do stuff ...
} finally {
AutoReleasePool.releasePool();
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
that seems even more complicated, but you do you. I don't know the whole picture anyway so I'm just talking smack here.
Real programmers use butterflies
|
|
|
|
|
Yeah, completely different scenario - I was dealing with potentially hundreds of thousands of COM objects that needed to be created from Java, so couldn't go with a single C memory reference.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
hai, i'm honey the monster, aka honey the codewitch
I've gotten a fair amount of confusion from people recently and over the past several months about how people refer to me, to wit my pronouns "he", "she", whatever. This has resulted in PMs (i don't mind) and/or deleted/edited/awkward comments and so I just want to clear things up.
I don't care what pronouns you use for me. Use whatever makes the most sense to you.
my gender is bees.
i'm just weird, but amicably so.
if i get too many more i'm putting this in my bio
Real programmers use butterflies
|
|
|
|
|
it's 'nice' of you to try and clear things up - I guess a lot of us struggle with gender fluidity and as you put it 'personal pronouns'
.. the only thought going through my tiny mind, was what if someone began a conversation 'hi honey ...'
seriously, seems like other people's issues, gender shouldnt come into it
|
|
|
|