Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5 With SWIG and Python

This thread is locked; no one can reply to it. rss feed Print
Allegro 5 With SWIG and Python
Billybob
Member #3,136
January 2003

I'm building an engine that uses Allegro 5.0.1, and is turned into a Python module through SWIG.

Python:

import IonEngine
IonEngine.init()

In turn, init() is a C function:

void init()
{
     if(!al_init()) {
          printf("Allegro Error: Could not init Allegro.\n");
          return;
     }
}

That works fine. However, when python exits, the DLL crashes. It raises an access violation in (_IonEngine.pyd!_CRT_INIT) at:
onexitbegin_new = (_PVFV *) DecodePointer(__onexitbegin);

If init() is not called, there is no crash. If al_uninstall_system() is called before exiting python, there is no crash.

Besides this issue, Allegro seems to work fine. I can add in display creation, draw some stuff, etc, etc. No crashes until exit.

So I have two options:
1) Figure out why it's crashing, and fix it.
2) or; have Python automatically call the engine's cleanup function.

Does anyone have any ideas as to why the crash is occurring?

_CRT_INIT is apparently called when the DLL is unloaded, and it is responsible for calling all the atexit functions (like the one Allegro registers). I'm not sure what is different between calling al_uninstall_system in _CRT_INIT vs. calling it before Python exits. :-/

Thomas Fjellstrom
Member #476
June 2000
avatar

Python has probably unloaded the dll before atexit in the CRT is actually called. Or that's what it sounds like.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Billybob
Member #3,136
January 2003

Python has probably unloaded the dll before atexit in the CRT is actually called.

Is that possible? How would python unload a DLL without causing Win32 to fire a PROCESS_DETACH event?

Elias
Member #358
May 2000

Instead of al_init I'd use al_install_system, that way you can make sure no atexit calls are made by Allegro.

--
"Either help out or stop whining" - Evert

Billybob
Member #3,136
January 2003

Another detail:

It crashes at d3d_disp.cpp:d3d_shutdown:2656:

_al_d3d->Release();

MSVC correctly reports that it has no idea what _al_d3d is.

So ... I kind of assume the same thing as TF, but I don't know how Python is unloading the DLL without triggering an event, or what I can do to remedy that. In fact, how is it calling d3d_shutdown if the DLL was indeed unloaded?

EDIT: Or perhaps D3D is being killed first?

Elias said:

Instead of al_init I'd use al_install_system, that way you can make sure no atexit calls are made by Allegro.

Yes, perhaps. I can also register an atexit in Python itself. I tested that, and it works. I'm assuming there is a programmatic way to do it on the C side. That's my Option 2 listed in the OP. It's a reasonable solution for this case, but it would be nice to discover the root cause and fix that, so as to assist and future travelers of this path.

EDIT2: Skipping the Release code above, and the bitmap destruction call, results in no crash. So, my best guess for now is that D3D is being unloaded before Allegro and invalidating whatever references Allegro is holding. So ... how do I fix that? :P

Go to: