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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:17:56+00:00 2026-05-18T08:17:56+00:00

Earlier today I asked this question . After spending some time investigating this issue,

  • 0

Earlier today I asked this question.

After spending some time investigating this issue, I have discovered what is going on. I am posting this as a new question because I think it is interesting enough to track as a separate issue. I will update that question with the answer (and a link to this one).

Launching unit test from debugger

// Construct object
Object* pObject = new Object(...);
// Pointer value of pObject == 0x05176960

// Lots of other code
// ...

// Destroy object
delete pObject;

// Construct object again
pObject = new Object(...);
// Pointer value of pObject == 0x05560194     /* Different memory location */

Launching unit test from command line

// Construct object
Object* pObject = new Object(...);
// Pointer value of pObject == 0x05176960

// Lots of other code
// ...

// Destroy object
delete pObject;

// Construct object again
pObject = new Object(...);
// Pointer value of pObject == 0x05176960     /* Same memory location */

In summary:

  • When launching the unit test from the command line, subsequent calls to new to allocate an Object (deleteing the previous Object before allocating a new one) always return the same address in memory.
  • When launching the unit test from the debugger, subsequent calls to new to allocate an Object (deleteing the previous Object before allocating a new one) always return a unique address in memory.

The problem is that because allocations of Object always get the same address in memory when launching through the command line, a map which I am accessing which has stored the old pointer can still be used and the test won’t crash. But I want my unit test to crash when the defect fix is not in place, to ensure that it doesn’t silently fail and the defect doesn’t come back.

There are 2 parts to my question:

  1. Why would the heap manager re-use the same part of memory when launching a unit test from the command line, but not when I launch the unit test from the debugger?

  2. Is there a compiler setting I could use on my test harness, or a method I can call to prevent the heap manager from re-using a section of memory that I have deleted, to allow me to correctly write my unit test? 1


1Obviously one way of doing this is to not delete the original object, but the part of the code that allocates this is in my production code, and I doing this would result in memory leaks.

  • 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-18T08:17:56+00:00Added an answer on May 18, 2026 at 8:17 am

    Your unit test is flawed, since it’s relying on undefined behavior. You should rewrite your unit test so that it doesn’t rely on undefined behavior, in which case it will always pass regardless of how the memory manager decides to allocate memory.

    What you’re doing is this:

    Object* pObject = new Object(...);
    ...
    delete pObject;
    pObject = new Object(...);
    // Use dangling pointer to first object, and if it crashes, the unit test fails
    // This is WRONG since a crash isn't guaranteed
    

    You should instead restructure the unit test so it works like this:

    Object* pObject = new Object(...);
    ...
    // Check to see if there are dangling references to pObject right before we
    // delete it.  If there are, assert() and fail the unit test.
    assert(NoDanglingReferences(pObject));
    delete pObject;
    // Continue on with more tests
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked this question earlier today but I didn't provide enough information and
I know I have asked this question in a different manner earlier today but
I asked this question a little earlier today but am not sure as to
This relates to another question I asked earlier today. I built SVN 1.6.2 from
Earlier today I asked this question which arose from A- My poor planning and
earlier today I asked this question . So since moq creates it's own class
I asked this question in the DataDynamics forum earlier today. I thought that maybe
I asked this goofy question earlier today and got good answers. I think what
I asked a question earlier today about singletons, and I'm having some difficulties understanding
This is a follow up to a question I asked earlier today regarding the

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.