On Sun, Sep 20, 2009 at 11:37 AM, Tamas Nepusz
<address@hidden> wrote:
So, would the following scenario result in memory corruption if the GIL was released?:
in python, create graph,
make a full copy of the graph (not a weakref),
in thread 1, with first graph, calculate something
in thread 2 with the copy of the graph, calculate something
thread 1 calculation fails,
error function/destructor is called, ...
thread 2's graph (the copy) gets freed?
Nope. The data structures created _during_ the calculation would possibly be freed in thread 2 (not the copy of the graph itself). So, for instance, let us assume that you are calculating betweenness centrality. Betweenness centrality requires a few temporary vectors to store partial result. What happens is:
1. In thread 1, the calculation starts with the first graph
2. Thread 1 allocates the temporary vectors and pushes the memory addresses down the stack
3. In thread 2, the calculation starts with the second graph
4. Thread 2 allocates the temporary vectors and pushes the memory addresses down the stack
5. Thread 1 finishes the calculation. Since thread 1 knows that it pushed, say, two addresses down the stack (for the temporary vectors -- this is just an example, so the numbers may be arbitrary), it pops the topmost two memory addresses from the stack. These addresses happen to belong to thread 2 since those were pushed later, so thread 1 could happily free the memory address that is being used by thread 2.