# Embed ruby in a threaded C++ Application (Mingw)

**URL:** <https://www.ruby-forum.com/t/embed-ruby-in-a-threaded-c-application-mingw/50272>\
**Category:** Ruby\
**Created:** [December 14, 2005, 6:58pm UTC](https://www.ruby-forum.com/t/embed-ruby-in-a-threaded-c-application-mingw/50272 "2005-12-14T18:58:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSutf-8SqSFrSC3SA9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bc79bd/32.png) [@SSSutf-8SqSFrSC3SA9](https://www.ruby-forum.com/u/SSSutf-8SqSFrSC3SA9)\
**Post date:** [December 14, 2005, 6:58pm UTC](https://www.ruby-forum.com/t/embed-ruby-in-a-threaded-c-application-mingw/50272/1 "2005-12-14T18:58:06Z")

</div>

I’ll try to explain the current status of my application :

I have a Qt4 Application which runs on Windows (thanks to Mingw).  
In one thread created by this application, I would like to use the Ruby  
1.8.2  
engine (to evaluate a script) [only in this thread which is created on  
the  
fly].

So, I create a thread which makes the following thinks :

- ruby\_init
- rb\_eval\_string(my script) [encapsulated in a rb\_protect mechanism]
- some rb\_funcalls [encapsulated in a rb\_protect mechanism]
- ruby\_finalize

And the thread is finished (and destoyed).

So, the first time I run my thread, all is perfect.  
But, the second time, a segmentation fault occurs in the rb\_eval\_string  
function. So, I suppose something wasn’t correctly cleaned by  
ruby\_finalize…

Is there another way to finalize the Ruby engine ? Is something missing  
?

Thank you
