ariia
ariia

Reputation: 103

Protobuf exception when allocating memory for string in dll

I am using protobuf 3 to serialize a simple message.

I get a bad alloc when i set a string value for one of the memebers of my protobuf message like so.

std::string a("eeee");
hello_in.set_name(a);

The bad alloc exception happens in the libprotobuf.dll in this function...

 void CreateInstance(Arena* arena, const ::std::string* initial_value) {
    GOOGLE_DCHECK(initial_value != NULL);
    // uses "new ::std::string" when arena is nullptr
    ptr_ = Arena::Create< ::std::string>(arena, *initial_value);
  }

But i think the real problem is that initial_value has been corrupted somehow and has a size of [size] = 3435973836.

Not sure how this is being corrupted. CreateInstance does get called a few times prior to this but its the first time it is called from main.cpp. Which leads me to believe that it has something to do with dll's and ownership of memeory.

Using any of the other set_name functions also cause a bad alloc exception. Setting the bool or int in the message works fine.

Here is the message and the main.cpp. I didnt include the hello.pb.h/pb.cc as they are quite big but can if it helps.

// See README.txt for information and build instructions.
//
// Note: START and END tags are used in comments to define sections used in
// tutorials.  They are not part of the syntax for Protocol Buffers.
//
// To get an in-depth walkthrough of this file and the related examples, see:
// https://developers.google.com/protocol-buffers/docs/tutorials

// [START declaration]
syntax = "proto3";
package commands;

import "google/protobuf/timestamp.proto";
// [END declaration]

// [START messages]
message Hello {
    string name = 1;
    int32 id = 2;  // Unique ID number for this person.
    bool on = 3;
    google.protobuf.Timestamp last_updated = 4;
}

// [END messages]

#include "hello.pb.h"

//  stl
#include <fstream>
#include <iostream>

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    commands::Hello hello_in;
    hello_in.set_id(2);
    std::string a("eeee");
    hello_in.set_name(a);    
    hello_in.set_on(false);

    {
        // Write the new address book back to disk.
        std::fstream output("hello.txt", std::ios::out | std::ios::trunc | std::ios::binary);
        if (!hello_in.SerializeToOstream(&output)) {
            std::cerr << "Failed to write address book." << std::endl;
            return -1;
        }
    }

    commands::Hello hello_out;

    {
        // Read the existing address book.
        std::fstream input("hello.txt", std::ios::in | std::ios::binary);
        if (!input) {
            std::cout << "hello.txt" << ": File not found.  Creating a new file." << std::endl;
        }
        else if (!hello_out.ParseFromIstream(&input)) {
            std::cerr << "Failed to parse address book." << std::endl;
            return -1;
        }
    }

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

Upvotes: 0

Views: 762

Answers (1)

Norbert Kees
Norbert Kees

Reputation: 11

I have observed same behavior (Visual Studio 2019 C++ project). The solution which helped me: libprotobuf.lib and libprotobuf.dll were replaced in debug/x86 mode by its debug version, libprotobufd.lib and libprotobufd.dll.

Upvotes: 1

Related Questions