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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:19:17+00:00 2026-06-02T08:19:17+00:00

I am using Google Protocol Buffers in the following way: void myfunc() { Buffers::MyBuffer

  • 0

I am using Google Protocol Buffers in the following way:

void myfunc() {
    Buffers::MyBuffer buf;
    buf.ParseFromArray(data, datalen);
    ...
    return;
}

The documentation for Protocol Buffers says that to free memory for a buffer that the object should be deleted. I’m no C++ genius, but I thought delete should only be called for objects allocated with new. Is memory cleaned up on return here?

  • 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-02T08:19:19+00:00Added an answer on June 2, 2026 at 8:19 am

    Memory for a buffer should be deallocated as the object is destroyed.

    void myfunc() {
        Buffers::MyBuffer buf; // this create your object on stack
        const char* data= new char[100];
        datalen= readSomeData( data, 100 ); // not best for file, socket, etc.
        buf.ParseFromArray(data, datalen); // this alocate any needed object
        // by calling Buffers::SomeInternalBuffer:New
        // now you may delete the data varible, is not needed
        delete[] data; // called new, so call delete
        ...
        buf.Clear(); // here the read data are lost,
        // but some objects may not be deleted for future use
    
        buf.mutable_someinternalbuffer(); // call Buffers::SomeInternalBuffer:New,
        // or may not if ParseFromArray already call it
    
        ...
        return; // here buf is out of scope and destroyed
        // any buffers that are created by New is going to be deleted
    }
    

    release_foo
    To keep buffer from deletion, use static New method:

    Buffers::MyBuffer* myfunc2() {
       Buffers::MyBuffer* buf = Buffers::MyBuffer:New();
       if( buf->ParseFromArray(data, datalen) ) 
           return buf; // return object, no memory dealocation
       delete buf; // something wrong with data, delete, destroy and dealocate
       return NULL; // return null 
    }
    

    If you need to keep any SomeInternalBuffer but not MyBuffer use release_<field> method:

    Buffers::SomeInternalBuffer* myfunc3() {
        Buffers::MyBuffer buf; // this create your object on stack
       if( buf.ParseFromArray(data, datalen) ) 
           return buf.release_someinternalbuffer(); // return object
       // if ParseFromArray do not call New, method release_someinternalbuffer  
       // will call New and copy data from default value
       // no memory deallocation for returned object, everything else will deallocate
    
       return NULL; // same as in first example
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using cocoaasyncsocket to send data Google Protocol Buffers (using http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers ) to a
I'm using Google Protocol buffers and am trying to determine the following: If my
Do you know about any example using together Google Protocol Buffers and GWT?
i have designed a protocol using google protocol buffers and now want to write
I'm trying to hack a client together in C++ using Google's Protocol Buffers and
I'm using Google Protocol Buffers to generate some Java classes for my project. Using
I was just looking through some information about Google's protocol buffers data interchange format.
I'm trying to write an application which uses Google's protocol buffers to deserialize data
The Weirdness I have compiled Google Protocol Buffers using no extra parameters for a
We are looking at using Google's Protocol Buffers to handle serialization between a c++

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.