473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why can't I destroy Py_Decref() the pointer?

Why can't I destroy Py_Decref() the pointer? It crashes because of that. Anyone could point to me what is wrong in this piece of code?

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <string>
  4. #include <Python.h>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. #include "BounceInterface_wrap.cxx"
  9.  
  10. PyObject * pModule = NULL, *pFunc = NULL;
  11. PyThreadState * mainThreadState = NULL;
  12.  
  13.  
  14. const char * line = "from time import time,ctime\nprint 'Today is',ctime(time())\n";
  15.  
  16. DWORD WINAPI Callback1( LPVOID lpvData)
  17. {    
  18.  
  19.  
  20.     for(int i = 0; i < 200; i++)
  21.     {
  22.  
  23.  
  24.         PyObject *pArgs, *pObject, *pMsg;
  25.         BounceResult Object;
  26.         pObject = SWIG_NewPointerObj((void*)&Object, SWIGTYPE_p_BounceResult, 1);                
  27.         pMsg = PyString_FromString("ASDASDSAD");
  28.  
  29.         PyObject * pValue = NULL;
  30.  
  31.         pArgs = PyTuple_New(2);
  32.  
  33.         PyTuple_SetItem(pArgs, 0, pObject);
  34.         PyTuple_SetItem(pArgs, 1, pMsg);
  35.  
  36.  
  37.         if(pArgs && pMsg && pObject)
  38.         {
  39.             //printf("Calling function...");
  40.             Py_BEGIN_ALLOW_THREADS;
  41.             pValue = PyObject_CallFunction(pFunc, "O", pArgs);
  42.             Py_END_ALLOW_THREADS;
  43.  
  44.         }
  45.         else
  46.         {
  47.             return NULL;
  48.         }
  49.  
  50.         if (!pValue) {
  51.             fprintf(stderr, "Cannot convert argument\n");
  52.             return NULL;
  53.         }            
  54.  
  55.  
  56.         if (pValue) {
  57.             Py_XDECREF(pValue);
  58.         }
  59.         else {
  60.             PyErr_Print();
  61.             fprintf(stderr,"Call failed\n");
  62.             return NULL;
  63.         }
  64.  
  65.         if(pObject != NULL)
  66.         {
  67.         //    Py_DECREF(pObject);;        If I uncomment this it'll crash
  68.         }
  69.         if(pArgs != NULL)
  70.         {
  71.         //    Py_DECREF(pArgs);            If I uncomment this it'll crash
  72.         }
  73.  
  74.  
  75.  
  76.     }
  77.  
  78.  
  79.     Sleep(100);
  80.     return TRUE;
  81. };
  82.  
  83.  
  84. int main(int argc, char *argv[])
  85. {
  86.     Py_Initialize();
  87.     PyEval_InitThreads();
  88.     SWIG_init();        
  89.     init_BounceCheck();    
  90.  
  91.     PyObject *pName = NULL;
  92.  
  93.     pName = PyString_FromString("bounce");
  94.     pModule = PyImport_Import(pName);
  95.     Py_DECREF(pName);
  96.  
  97.     if (pModule != NULL) 
  98.     {
  99.         pFunc = PyObject_GetAttrString(pModule, "CheckBounce");
  100.  
  101.         if (!pFunc || !PyCallable_Check(pFunc)) 
  102.         {
  103.             printf("ERROR\n");
  104.            //Py_DECREF(pFunc);
  105.            system("PAUSE");
  106.         }
  107.     }
  108.  
  109.     const int nHandles = 1000;
  110.     HANDLE hHandles[nHandles];
  111.     DWORD threadID[nHandles];
  112.  
  113.  
  114.  
  115.     for(int i = 0; i < nHandles; i++)
  116.     {        
  117.         hHandles[i] = CreateThread(NULL,0 ,Callback1,(LPVOID)NULL,0,&threadID[i]); 
  118.     }
  119.  
  120.     cout << "Waiting for the threads to end" << endl;
  121.  
  122.     for(int i = 0; i < nHandles; i++)
  123.     {
  124.         WaitForSingleObject(hHandles[i],INFINITE);
  125.         CloseHandle(hHandles[i]);
  126.     }
  127.     cout << "Threads are finished...\n";
  128.     // shut down the interpreter    
  129.  
  130.  
  131.     Py_Finalize();
  132.     system("PAUSE");
  133.     return 0;
  134. }
  135.  
May 31 '10 #1
0 909

Sign in to post your reply or Sign up for a free account.

Similar topics

30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
2
by: Martin Heuckeroth | last post by:
Hi, How can we set and read the pointer in a listbox or a combibox to the found search. We use the datarow function with SQL2000 and VB.NET. Hope anyone can help Regards, Martin
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
6
by: laikon | last post by:
Hi, everyone, below is my program to test static pointer data member; class A { private: static A* p; protected: A() {} public: static A* init()
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
3
by: desktop | last post by:
Why does the value of the below int pointer not disappear after using destroy: std::allocator<intallo; int* ip; allo.construct(ip,777); std::cout << "*ip = " << *ip << std::endl; ...
3
by: ASWINIGANGADHARAM | last post by:
hi all, i am studying engg,i have problem that ,how can i convet from the file pointer to character pointer or character array.in my project i cant use file pointer,i need to convert file pointer...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.