Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8384373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:23:26+00:00 2026-06-09T17:23:26+00:00

I have a matrix type which contains a void* array, representing an array of

  • 0

I have a matrix type which contains a void* array, representing an array of objects (which are all of one type in a given matrix, e.g., all C integers, all floats, doubles, a variety of structs, or possibly even all Ruby VALUEs).

Memory allocation and garbage collection seems to work correctly until I try to create a matrix of VALUEs.

I have the following mark function defined:

void mark_dense_storage(void* s) {
  size_t i;
  DENSE_STORAGE* storage = (DENSE_STORAGE*)s;
  if (storage && storage->dtype == RUBY_OBJECT)
    for (i = 0; i < count_dense_storage_elements(s); ++i)
      rb_gc_mark(*((VALUE*)(storage->elements + i*sizeof(VALUE)));
}

So it only does marking if it’s actually a VALUE matrix — otherwise, NULL gets passed to Data_Wrap_Struct for the mark function.

But I’m getting a segfault when I test some of the VALUE matrix functions (see gist).

Specifically, it seems to segfault the first time I try to call a Ruby method on the very first object in the VALUE* array:

C[i+j*ldc] = rb_funcall(C[i+j*ldc], nm_id_mult, 1, beta); // C[i+j*ldc] = C[i+j*ldc]*beta

nm_id_mult is a global defined in my Init function as rb_intern("*").

It’s possible this isn’t a garbage collection problem, but the GC is the part of Ruby that I understand the least — and my segfault is also almost identical to this trace, which the poster attributes to the GC.

So, my questions:

  1. If it’s the GC, what’s the appropriate way to mark an array of VALUEs?

  2. If it’s not the GC, how do I go about diagnosing this type of error? I’ve never seen anything like it.

EDIT:

Turns out that this is an example of failure to initialize VALUEs created in C.

In other words, make sure to do *(VALUE*)a = INT2FIX(0) before you try to access a.

I do still think the question is relevant. I haven’t managed to find any really good examples of marking for clean-and-sweep garbage collection, on StackOverflow or elsewhere. If you can provide such an example and/or explanation, I’ll mark that as a correct answer to this question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:23:28+00:00Added an answer on June 9, 2026 at 5:23 pm

    Ruby’s mark-and-sweep GC works in two stages.

    The first stage marks live objects. It works recursively by calling the marking function of each known “live objects”. The initial set of live objects is produced by scanning the C stack of each known Ruby thread or of each registered global objects (there’s a C function to register/deregister “known live” objects). The marking function of object X should then call rb_gc_mark for each objects that X refers to. In other words, what you do is exactly what you should do.

    However, as you noticed later, not any possible VALUE is a valid Ruby object. However, I believe that initializing with Qnil (i.e. nil) would be more ruby-ish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a matrix in the type of a Numpy array. How would I
I have a matrix of objects that contains data in this form: name A,2,name
I have an array of arrays P, which represents a Matrix, as an array
I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to
I have a text file which contains matrix of N * M dimensions. For
I have the following problem: I have this multi-level array (nested array) which contains
I have a 2D matrix myMatrix of integers which I want to save its
I have the following code to convert the type of the matrix using cv::Mat.convertTo()
I have a matrix A which I want to convert into a data.frame of
I have a large sparse matrix representing attributes for millions of entities. For example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.