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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:11:57+00:00 2026-06-16T17:11:57+00:00

I have a design issue I am trying to resolve. In short, is it

  • 0

I have a design issue I am trying to resolve. In short, is it ok to use finalize() to release private static resources that pertain only to that object, and are only of use for as long as that object lives? Details follow…

First here are the constraints:

  1. I am using the decorator pattern to encapsulate a type (X) that I
    didn’t write

  2. X is final so subclassing it is out of the question.

  3. I have a method in the wrapper, say x(), that at any time will
    return the underlying X instance

  4. The reason for x() is that the wrapper type needs to be
    interchangeably compatible with existing API’s that expect an X
    parameter

  5. The wrapper has a constructor that takes an X instance as a
    parameter. It then sets its encapsulated X object to be this
    instance.

The problem:

I want to add some functionality/data in the wrapper, BUT have it preserved. By this I mean, when the wrapper is ‘unwrapped’ to X, it can then be ‘rewrapped’ to the wrapper at some other point in the code, and hey presto, the extra data is restored even though it is not carried in the underlying X.

My thoughts so far:

If I create a static map in the wrapper class where the key is a unique X id, and the value is this extra data in some structure, then I can retrieve it inside the rewrap(X x) method let’s say.

I won’t use the wrapper or X object as the key in the map as this will stop it ever possibly being GC’d (unless explicitly removed from the map), so instead we’ll use unique hashcodes.

Whilst this seems OK in principle, the issue is when to delete this extra data from the static map. In this case can we permit an implementation of finalize():

finalize(){
   map.remove(wrapperKey);
}

Here’s my justification… We all know relying on finalize() is a bad idea to release resources in general, but here the resources in question pertain directly to the object. There is no reference to external resources, and so we need these internal resources until the wrapper object itself is GC’d, at which point we just delete the resources mapping.

The worst case scenario would be not worry about deleting these static mappings that are redundant, as this results in memory leaks.

I can’t think of any other way to achieve this functionality and preserve the backwards compatibility with existing APIs:

public void foo(Wrapper wrapper){
  bar(wrapper.x());
}
public void bar(X instance){...}

So that’s the problem, any alternative approaches or opinions?

Many thanks

EDIT:
After further research I thought I would update this question as similar situations may be prime candidates for weak references

  • 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-16T17:11:58+00:00Added an answer on June 16, 2026 at 5:11 pm

    If you explicitly call Wrapper.x every time you need access to the X instance, couldn’t you add a cleanup method to Wrapper and explicitly call that before getting rid of the Wrapper object?

    The cleanup method could be called from finalize too, to ensure that it gets cleaned up if it hasn’t been called explicitly. This would give you the best of two worlds, as it would give you a greater chance of reducing memory leaks if the Wrapper is used incorrectly.

    void someRandomMethod() {
        Wrapper someWrapperINeed = new Wrapper(new X(blah, blah blah));
        foo(someWrapperINeed);
        someWrapperINeed.clean();
    
        // Instead of foo(new Wrapper(new X(blah, blah, blah));
    }
    
    // Or foo can call clean if the wrapper will never be needed after its invocation
    void foo(Wrapper w) {
        bar(w.x());
        w.clean();
    }
    

    Edit: Added code sample!

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

Sidebar

Related Questions

I am trying to resolve a design issue implementation in typo3. Essentially I have
I'm currently having an issue where I have a javascript object that is trying
I'm trying to use domain driven design while creating a website that is publicly
I have a weird database design issue that I'm not sure if I'm doing
I have run into a bit of a design issue with my code. I
I'm having a design/css issue. I'm using Twitter Bootstrap and have a .row-fluid, with
I have a design for a page that uses 3 types of audio and
I have to design a data structure that is to be used in a
i have a design issue regarding sending user data from a mobile phone app
I have been trying to use code-first migrations, and had some limited success, but

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.