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 8684167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:14:08+00:00 2026-06-12T22:14:08+00:00

I get invalid read / segmentation fault errors using boost::iterator_range . Where is my

  • 0

I get invalid read / segmentation fault errors using boost::iterator_range.
Where is my data going out of scope and how can I prevent that from happening?

Here is some code to reproduce the problem:

#include <iostream>
#include <vector>
#include <map>

#include <boost/shared_ptr.hpp>
#include <boost/range.hpp>
#include <boost/range/iterator_range.hpp>

These are my types:

typedef double Value;
typedef std::vector<Value> vValue;
typedef boost::iterator_range<std::vector<Value>::iterator> rValue;

Utility function:

void print_range(const rValue &r) {
    for(rValue::difference_type i = 0; i < r.size(); ++i) std::cout << r[i] << " ";
    std::cout << std::endl;
}

This is an object that stores a cache of all data.

class MyDataObject { // This object stores ALL DATA.
private:
    vValue data;

public:
    void setData(vValue data) {
        this->data = data;
    }
    vValue &getData() {
        return data;
    }

};

Data segments are then created as a subset of all data using boost::iterator_range.

class DataSegment { // This object points to a subset of all data using boost::iterator_range
private:
    rValue data;

public:
    void setData(rValue data) {
        this->data = data;
    }
    rValue& getData() {
        return data;
    }
};

Actual database implementation:

class DB { // The database caches ALL DATA and then returns a subset using boost::iterator_range when asked for.
private:
    std::map<std::string, MyDataObject> cache;

public:
    DB() {
        //
    }

    MyDataObject loadIntoCache(std::string key) {
        vValue data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        MyDataObject obj;
        obj.setData(data);

        cache[key] = obj;
        return obj;
    }

    boost::shared_ptr<DataSegment> getMemoryEfficientSubset() {
        MyDataObject obj = loadIntoCache("bar"); // If bar is not in cache, load it.

        rValue data = obj.getData();

        boost::shared_ptr<DataSegment> segment(new DataSegment());
        rValue out = boost::make_iterator_range(data.begin() + 2, data.begin() + 7);
        segment->setData(out);

        return segment;
    }
};

Test code:

int main() {
    DB *db = new DB();

    boost::shared_ptr<DataSegment> segment = db->getMemoryEfficientSubset();
    print_range(segment->getData()); // ERROR: segfault within print_range. Valgrind says "Invalid read of size 8"

    delete db;
    return 0;
}
  • 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-12T22:14:09+00:00Added an answer on June 12, 2026 at 10:14 pm

    The iterator range ultimately stored in *segment is associated with data, which is local to the call. Presumably you meant loadIntoCache to return a MyDataObject& (initialized from e.g. cache[key] = obj), which would remain valid as long as the cache is. Make also sure that obj is a reference to the result of the call to loadIntoCache, not a copy, and similarly for data and the result of the call obj.getData().

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

Sidebar

Related Questions

Possible Duplicate: Invalid read/write sometimes creates segmentation fault and sometimes does not I have
I've read about this a lot and I just can't figure it out. It
I tried to read out data from a specific date from a MySQL database.
I want to redirect to home page if session get invalid. My spring-servlet.xml is
I've searched all over and tried everything but I still get: invalid multibyte char
when I seem to use parameters in my query, I get an error Invalid
I get The ORDER BY clause is invalid in views, inline functions, derived tables,
I get the following exception java.lang.NumberFormatException: Invalid int: It is happening when you try
I get the following error when running the code below: invalid byte sequence in
I get error: ORA-06575: Package or function GET_CONC_NAMES is in an invalid state When

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.