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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:06:39+00:00 2026-05-27T09:06:39+00:00

Suppose I have a following constructor in C++ class: MyClass::MyClass() { char* buffer =

  • 0

Suppose I have a following constructor in C++ class:

MyClass::MyClass()
{
    char* buffer = malloc(100);
    if (0 != someD3DXCallThatCanFail(..., buffer, ...))
    {
        free(buffer);
        throw MyException(L"some message");
    }
    char* buffer2 = malloc(200);
    if (0 != anotherD3DCallThatCanFail(..., buffer2, ...))
    {
        free(buffer);
        free(buffer2);
        throw MyException(L"another message");
    }
    .. more code here where buffer and buffer2 are still used

    free(buffer);
    free(buffer2);
}

EDIT: I do hate malloc/free and new/delete, but unfortunately I need to use buffers for loading textures which are then passed over to ID3D10ShaderResourceView, ID3D10Buffer, vertex buffer and the like. All those require pointers to a buffer.

What I am trying to do it to use exceptions instead of returning error codes.
Also I wish to create buffers where they are needed and free them right after they are no longer needed.

However, what looks ugly is that in case of errors, no matter if I return error codes or throw exceptions, I still should remember to clean up any buffer that was created up to that point. If I have 10 buffers and 10 possible points of error, I will have to call free() 100 times (in each error case remember to free each buffer).

Now suppose I or worse, my colleague wants to change some logic and, say, adds another buffer somewhere in the middle. Now, he’d have to look thru all the errors that can happen in the rest of the method and add free() for that buffer at every such place. If he’s in a hurry, he can easily overlook a few such places, and you got a memory leak.

This also bloats the code immensely.

finally keyword would solve that problem in Java or C#. No matter where in the code the exception happened, I’d still clean up all those buffers in “finally” (btw you wouldn’t need that with garbage collection). In C++ from what I understand, I may have to create a member variable for any such buffer, and in the destructor make sure the buffers are cleaned up. Looks also quite ugly to me, since a member variable with name “pBuffer”, even a private one, is just garbage, since it’s only used in the one method (in this case, constructor) and will always be NULL the rest of the time.

Must be a common problem, however I didn’t manage to find an answer using search. Thanks!

  • 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-05-27T09:06:40+00:00Added an answer on May 27, 2026 at 9:06 am

    Stop managing memory manually and you won’t have these sorts of problems. Use something like std::vector<char>.

    Alternatively, you could use something like Boost’s shared_array, but that’s overkill for what you’re trying to do here:

    http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/shared_array.htm

    The more general point to be made here is that you should use the RAII idiom – that is, when you acquire resources, you store them in an instance of a class whose destructor frees them again. Then, however an instance of that resource-owning class goes out of scope, the resources are guaranteed to be freed.

    See here:

    http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization

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

Sidebar

Related Questions

Suppose we have following two classes: class Temp{ public: char a; char b; };
Suppose I have some Message class like the following. (This is a made-up class
Suppose I have the following Scala code: class Foo(a: Int) class Bar(b: Buffer[Int]) extends
Suppose I have the following C# class class MyClass { private int _i; private
Suppose I have following template class: template<unsigned char I, unsigned char F> class FOO
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
Suppose I have the following declaration: class Over1 { protected: class Under1 { };
Suppose I have the following class heirarchy: class A() class B(a:A) class C(b:B) class

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.