0

I'm trying to embedd python interpreter in C++ app using Python/C API. In my code exist something like that:

string code("python code here");
PyRun_SimpleString( code.c_str() );

Python/C API function expect const char * argument. This code is executed in loop and my app sometimes crashes on PyRun_SimpleString command. The crash is always on first loop iteration, I guess I haven't encountered an error in the next ones. Sometimes I can run this loop many times (for example 50) and sometimes fails on first try.

Is my way of passing an argument incorrect or it is good and I should try to find memory management error in the rest of my code?

Debugger info:

Thread #1 [0_FULL_GAME] 16044 [core: 0] (Suspended : Signal : SIGSEGV:Segmentation fault)   
malloc_consolidate() at malloc.c:4 492 0x7ffff7513be3   
_int_malloc() at malloc.c:3 699 0x7ffff7515e03  
__GI___libc_malloc() at malloc.c:3 066 0x7ffff7518419   
0x7ffff7c74458  
PyArena_New() at 0x7ffff7baf423 
PyRun_StringFlags() at 0x7ffff7ba7648   
PyRun_SimpleStringFlags() at 0x7ffff7ba8969 
Spectacle::update() at spectacle.cpp:144 0x555555590a5d 
State::process() at state.cpp:6 0x5555555981be  
Game::run() at game.cpp:25 0x55555557a3ce   
main() at main.cpp:6 0x55555557f622 

SOLVED: The error was caused by writting array element out of range in different place in code.

IzZy
  • 364
  • 3
  • 16

1 Answers1

2

Is my way of passing an argument incorrect or it is good and I should try to find memory management error in the rest of my code?

Your way of passing a raw C string is correct.

Crash inside malloc often indicates that the program has somehow damaged internal heap structures. See if the program did anything bad (like writing to a freed memory block or writing outside of allocated block) before those two lines are executed.

Igor G
  • 1,838
  • 6
  • 18