I was able to cast the cache listener's afterCreate event value to the PdxInstance data type when the region was updated through Geode's REST API. The code below ran fine without any issues. However when I tried updating the region through the custom defined pdx serialization using the Order.hpp and Order.cpp example code that Geode provided, the dynamic cast in my defined cache listener below returned a value with memory address 0x0 and so when it tries to access the fields in the order object, it's giving me segmentation fault. The objectSize() method also returned 0 bytes which suggests to me that the pdx serialisation was not done correctly.
Would someone be able to advise me what I've done incorrectly below?
#include <iostream>
#include <sstream>
#include <string>
#include <geode/CacheFactory.hpp>
#include <geode/PoolManager.hpp>
#include <geode/RegionFactory.hpp>
#include <geode/RegionShortcut.hpp>
#include <geode/TypeRegistry.hpp>
#include <geode/AttributesMutator.hpp>
#include <geode/CacheListener.hpp>
#include <geode/EntryEvent.hpp>
#include <geode/CacheableString.hpp>
#include <geode/PdxInstance.hpp>
#include "Order.hpp"
using namespace apache::geode::client;
using namespace customserializable;
class MyEventHandler : public CacheListener
{
public:
void afterCreate(const EntryEvent& event) override
{
auto key(dynamic_cast<CacheableString *>(event.getKey().get()));
std::cout << "afterCreate: " << key->value() << std::endl;
auto mypdx = event.getNewValue().get();
mypdx->toString();
mypdx->objectSize();
PdxInstance *pdx = dynamic_cast<PdxInstance *>(mypdx);
// auto pdx(dynamic_cast<PdxInstance *>(mypdx));
int order_id = pdx->getByteField("order_id");
int quantity = pdx->getByteField("quantity");
std::string name = pdx->getStringField("name");
std::cout << "Testing" << std::endl;
}
};
auto MyListener = std::make_shared<MyEventHandler>();
int main(int argc, char** argv) {
auto cache = CacheFactory()
.set("log-level", "none")
.setPdxReadSerialized(true)
.create();
cache.getPoolManager()
.createFactory()
.addLocator("localhost", 10334)
.setSubscriptionEnabled(true)
.create("pool");
auto regionFactory = cache.createRegionFactory(RegionShortcut::CACHING_PROXY);
auto region = regionFactory.setPoolName("pool").create("custom_orders");
cache.getTypeRegistry().registerPdxType(Order::createDeserializable);
region->getAttributesMutator()->setCacheListener(MyListener);
std::cout << "Create orders" << std::endl;
auto order1 = std::make_shared<Order>(1, "product x", 23);
auto order2 = std::make_shared<Order>(2, "product y", 37);
region->registerAllKeys();
region->put("Order19" , order1);
cache.close();
}