[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Problem with Pygame c api (bad crashes)



Hi,

this happens when 'pygame.h' is included into a '.cpp' file (or when
the VC++ /Tp or /TP switches are used).

Something to do with enhanced ANSI conformance or something ...

I find it bizzare that code that isn't even reported as a warning on
the max. warning level is reported as an error if the file extension
changes!


The code in question arranged into a more readable form:
...
PyObject *module = PyImport_ImportModule("pygame.base");

if (module != NULL)
{
  PyObject *dict  = PyModule_GetDict(module);
  PyObject *c_api = PyDict_GetItemString(dict, PYGAMEAPI_LOCAL_ENTRY);

  if( PyCObject_Check(c_api) )
  {
    int    i;
    void** localptr = (void*)PyCObject_AsVoidPtr(c_api);

    for(i = 0; i < PYGAMEAPI_BASE_NUMSLOTS; ++i)
      PyGAME_C_API[i + PYGAMEAPI_BASE_FIRSTSLOT] = localptr[i];
  }
  Py_DECREF(module);
}
...

This line *is* a bit strange:

    void** localptr = (void*)PyCObject_AsVoidPtr(c_api);
    ^^^^^^             ^^^^^
On general principle, shouldn't that be:

    void** localptr = (void**)PyCObject_AsVoidPtr(c_api);
    ^^^^^^             ^^^^^^

[I suspect the 'C' compiler is quiet here only because the types involve
void]

I think 'PyCObject_AsVoidPtr(c_api)' is actually returning a "pointer
to an array of function pointers" which are copied into the PyGAME_C_API
array, so a cast to (void**) is not a lie.


This is wrong:

void** localptr;
localptr = new void*;
*localptr = (void*)PyCObject_AsVoidPtr(c_api);

don't do it!

Try changing the (void*) in the original macro to a (void**),

good luck!
John.


----- Original Message -----
From: "Riccardo Trocca" <rtrocca@libero.it>
To: <pygame-users@seul.org>
Sent: Wednesday, August 14, 2002 10:41 AM
Subject: [pygame] Problem with Pygame c api (bad crashes)


> Hello I'm developing a python extension using SWIG and c++ on win32 with
> the latest pygame.
> I have some problems with the compilation (with distutils) due to
> import_pygame_base() and import_pygame_surface().
> The fact is that in C++ the compiler (VC 6 SP5) complains about "void**
> localptr = (void*)PyCObject_AsVoidPtr(c_api);"
> Well, I decided to try to fix this.
> Therefore I changed those lines in pygame.h (for import_pygame_base()) in:
> int i; void** localptr;\
> localptr=new void*;\
> *localptr= (void*)PyCObject_AsVoidPtr(c_api); \
>
> This works. I had to allocate explicitily localptr because otherwise I
> had a warning saying that I was using an unitialized var, and then
> python crashed as soon as I imported my module.
>
> Anyway the pains starts when I import_pygame_surface().
>
> I had to change those lines again and I obtained a:
>
> #define import_pygame_surface() { \
>     PyObject *module = PyImport_ImportModule("pygame.surface"); \
>     if (module != NULL) { \
>         PyObject *dict = PyModule_GetDict(module); \
>         PyObject *c_api = PyDict_GetItemString(dict,
> PYGAMEAPI_LOCAL_ENTRY); \
>         if(PyCObject_Check(c_api)) {\
>             int i; void** localptr;\
>             localptr=new void*;\
>             *localptr= (void*)PyCObject_AsVoidPtr(c_api); \
>             for(i = 0; i < PYGAMEAPI_SURFACE_NUMSLOTS; ++i) \
>                 PyGAME_C_API[i + PYGAMEAPI_SURFACE_FIRSTSLOT] =
> localptr[i]; \
>     } Py_DECREF(module); } \
>     module = PyImport_ImportModule("pygame.surflock"); \
>     if (module != NULL) { \
>         PyObject *dict = PyModule_GetDict(module); \
>         PyObject *c_api = PyDict_GetItemString(dict,
> PYGAMEAPI_LOCAL_ENTRY); \
>         if(PyCObject_Check(c_api)) {\
>             int i; void** localptr;\
>             localptr=new void*;\
>             *localptr= (void*)PyCObject_AsVoidPtr(c_api); \
>
>             for(i = 0; i < PYGAMEAPI_SURFLOCK_NUMSLOTS; ++i) {\
>                 printf("slot %d\n",i);\
>                 PyGAME_C_API[i + PYGAMEAPI_SURFLOCK_FIRSTSLOT] =
> localptr[i]; \
>             }\
>             printf("4\n");\
> } Py_DECREF(module); } }
>
> The problem is that this code charshes badly.
> I' ve found that  the first part (pygame.surface) works, then when we
> are in pygame.surflock, python crashes when i==2.
> I've set NUMSLOTS to 2, and I can import the module without crashes, but
> I still didn't try to use it.
> What do you think?
> Any workaround?
> Would it be possible to change pygame.h in order to make it work with VC6?
> I can contribute those patches, but I need help to fix this, bacause I'm
> stuck.
>
> Riccardo
>
>
>
> ____________________________________
> pygame mailing list
> pygame-users@seul.org
> http://pygame.seul.org

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org