transaction_scope_error

18 views
Skip to first unread message

Supriya Mishra SVNIT

unread,
Jul 11, 2023, 1:44:56 PM7/11/23
to pmem
Hi,

I am trying to create object of a class using "make_persistent<>()", and  I am getting transaction_scope_error with message "refusing to allocate memory outside of transaction scope".  I can't figure out how to do it. Below I explain the scenario with code snippet:

1. tree.hpp file: In this file, There is a Node class, which has some functions, like insert, search

#include<iostream>

#include<libpmemobj++/p.hpp>
#include<libpmemobj++/pool.hpp>
#include<libpmemobj++/persistent_ptr.hpp>
#include<libpmemobj++/make_persistent_atomic.hpp>
#include <libpmemobj++/transaction.hpp>
#include<libpmemobj++/make_persistent.hpp>

using namespace pmem;
using namespace pmem::obj;
class Node{
private:
     //some variables
      //some functions
public:
       //some variables
       //some functions
       void insert(int data);
};

2. main.cpp file: In this class, I want to create an object of Node class and want to insert some data into Node.

#include"tree.hpp"
int main(void){
pool<Node>pop;
persistent_ptr<Node>root;
try {
if (file_exist(path) != 0) {
pop = pool<Node>::open(path, LAYOUT);
} else {
pop = pool<Node>::create(path, LAYOUT, POOLSIZE,
S_IWRITE | S_IREAD);
}
root=pop.root();
} catch (const pmem::pool_error &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return;
} catch (const pmem::transaction_error &e) {
std::cerr << "Exception: " << e.what() << std::endl;
return;
}
        root=make_persistent<Node>();  // this line 19, throws
        root.insert(data);                           //pmem::transaction_scope_error exception
return 0;
}

when I change line 19 as:
transaction::run(pop,[]{

});

Supriya Mishra SVNIT

unread,
Jul 11, 2023, 1:52:33 PM7/11/23
to pmem
Continued...
transaction::run(pop,[]{
       root=make_persistent<Node>();  //this line causes compile time error: ‘root’ is not captured
});

How to create class object in such scenario? Can anyone help?

Best regards,
Supriya

Igor Chorążewicz

unread,
Jul 12, 2023, 12:00:41 AM7/12/23
to Supriya Mishra SVNIT, pmem
Hi,

I would suggest just trying out the examples in https://github.com/pmem/libpmemobj-cpp/blob/master/examples

Specifically, https://github.com/pmem/libpmemobj-cpp/blob/master/examples/transaction/transaction.cpp#L53 is what you are looking for.
Notice the `&` sign on line 58 that you are missing: that's why you are getting the compile time error. More information: https://en.cppreference.com/w/cpp/language/lambda

You can also use transactions without using lambda expressions (see transaction::manual and transaction::automatic examples).

Best regards,
Igor


--
You received this message because you are subscribed to the Google Groups "pmem" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pmem+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pmem/f4791c84-f2c4-45b0-8fe5-1a970c80273dn%40googlegroups.com.

Supriya Mishra SVNIT

unread,
Jul 12, 2023, 6:08:30 AM7/12/23
to pmem
Hi Igor,

Thanks a lot! I will go through the suggested resources and try again. If I have further issues/queries, I will get back to you.

Thanks again!

Best regards,
Supriya
Reply all
Reply to author
Forward
0 new messages