Ruby and Win32Api, How Long Does Data Stick Around?

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
Forest Goose
Posts: 377
Joined: Sat May 18, 2013 9:27 am UTC

Ruby and Win32Api, How Long Does Data Stick Around?

Postby Forest Goose » Wed Mar 04, 2015 7:21 am UTC

I'm not a programmer, I just occasionally code, so I'm a little out of my element with this question and would like to find the answer rather than mucking around and hoping I'm right (if that makes sense).

Two Questions:

1.) If I write a dll that, say, has a method to read in some data from a file and other methods that do stuff to it. After calling the file read method from Ruby, will that data still be "there" for the processing methods to work on? Or is it the case that, essentially, everything starts fresh with each win32api call?

2.) Suppose I have a method that returns a pointer and takes a pointer, if I return the pointer with my api call, then pass it back in, can I be certain that nothing else will do something with what is in memory there?

I'm sorry if those are poorly worded. I can grok C/C++ well enough to get it to do what I want, but I only touch it once in a blue moon - if anything is unclear, I can definitely clarify, or try to. Ultimately, I'm looking to use C to handle a particularly slow computation, but I need it to have the information it is working on remain around for multiple calls (I can't do it all in one shot) and trying to work with the structure of a ruby array from C just sounds unpleasant (so, I'm hoping that I don't have to bother with that, ultimately).
Forest Goose: A rare, but wily, form of goose; best known for dropping on unsuspecting hikers, from trees, to steal sweets.

jareds
Posts: 436
Joined: Wed Jan 03, 2007 3:56 pm UTC

Re: Ruby and Win32Api, How Long Does Data Stick Around?

Postby jareds » Wed Mar 04, 2015 10:07 pm UTC

FYI, I have no particular knowledge about Ruby, but I have good knowledge of the general principles involved in an interpreted language with a C foreign function interface (FFI).
Forest Goose wrote:1.) If I write a dll that, say, has a method to read in some data from a file and other methods that do stuff to it. After calling the file read method from Ruby, will that data still be "there" for the processing methods to work on? Or is it the case that, essentially, everything starts fresh with each win32api call?

The short answer is "it will almost certainly still be there".

There may be some terminological confusion. The term for calling a C function in your DLL from Ruby should be something like "foreign function call", not "win32api call". The Win32 API is a collection of OS functions that you can call directly from C. Any individual C function that you include in your DLL might make zero Win32 calls, one Win32 call, or many Win32 calls, but this has no relationship to data persistence.

While "there" is nebulous, I can't imagine anything that you'd be referring to that would not persist between foreign function calls (aside from things that wouldn't persist even if you were doing a straight C-to-C call, like (non-static) function local variables). You could lose data if the DLL is unloaded, but if Ruby unloads the DLL in between foreign function calls, I would consider that a surprising defect in its FFI.
Forest Goose wrote:2.) Suppose I have a method that returns a pointer and takes a pointer, if I return the pointer with my api call, then pass it back in, can I be certain that nothing else will do something with what is in memory there?

I am interpreting this as a question about how garbage collection (which may move things around in memory) interacts with a C FFI. That is, there is no enforcement mechanism that prevents other code in the process that loads your DLL from doing something with its memory due to bugs or whatever, so I'm answering about what you should rely on in the absence of bugs.

If your memory address came from the C side, for example if it is the address of a global variable in your DLL or it is an address you obtained from malloc, the address will never change. If the address came from the Ruby side, for example by passing a Ruby array to your DLL, you should not rely on the address of that Ruby array remaining the same between foreign function calls unless someone with Ruby knowledge tells you otherwise. Some garbage collected languages offer you the ability to "pin" native objects so they don't move.

User avatar
Forest Goose
Posts: 377
Joined: Sat May 18, 2013 9:27 am UTC

Re: Ruby and Win32Api, How Long Does Data Stick Around?

Postby Forest Goose » Fri Mar 27, 2015 9:05 am UTC

I'm so so so sorry, I posted this question half asleep, then forgot I ever did! (I rarely come to this section...) Thank you very much for your reply - apologies again.

I feel like such an ass, it drives me a little nuts when people just disappear after I answer something for them, I absolutely had no such intent - thank you, sincerely, for your answer:-)
Forest Goose: A rare, but wily, form of goose; best known for dropping on unsuspecting hikers, from trees, to steal sweets.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 5 guests