0

I have a strategy pattern, and would like to run it in a main loop in a game for example. The problem is there will be a memory leak If I'm not deleting the instance, and I also would like to use that instance somewhere else. How can I deal with memory allocation/deallocation in a strategy pattern.

CompressionContext *ctx = new CompressionContext();
//we could assume context is already set by preferences
ctx->setCompressionStrategy(new ZipCompressionStrategy());    
//get a list of files
ctx->createArchive(fileList);    
Moaz ELdeen
  • 249
  • 2
  • 3
  • 10
  • 3
    Can you explain terms like "strategy pattern" rather than assuming that everyone will know what it is and have the same understanding of it? – Kerrek SB Jul 27 '12 at 15:04
  • @yurikilochek sorry I fixed it. – Moaz ELdeen Jul 27 '12 at 15:06
  • 1
    @KerrekSB A Strategy pattern, is the pattern that can be used to select one algorithm from many algorithms at runtime depends on an algorithm – Moaz ELdeen Jul 27 '12 at 15:08
  • 4
    There is nothing special about the fact that the objects are implementations of a given strategy. You are asking about how to manage memory in C++, and the answers are always the same: RAII, smart pointers and the concept of "ownership" and lifetime of an object. – akappa Jul 27 '12 at 15:10
  • @KerrekSB you didn't, replace `.` with `->` – yuri kilochek Jul 27 '12 at 15:10
  • I did. BTW, this question, as it stands, cannot be answered. Going to vote for closing it. – akappa Jul 27 '12 at 15:13

1 Answers1

4

Use an std::shared_ptr<CompressionContextBase> instead of a CompressionContextBase* (i.e. a raw pointer).


Edit: This is just a suggestion, based on the information you provided, there may be other smart pointer implementations with different semantics, such as e.g. unique_ptr, which might be more suited. As @akappa suggests, you may want to read up on the topic more, to make a better decision -- again, based on the information in the question, you probably want a shared_ptr but there might be additional considerations you omitted.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • I'm not really sure he did understood what's the point, but... whatever. – akappa Jul 27 '12 at 15:16
  • @akappa You can explain what is the point please :) – Moaz ELdeen Jul 27 '12 at 15:17
  • 1
    @akappa: Well, that's why I added the link to cppreference, where you can read up on the matter. Why should everything as basic as this be repeated over and over again? – bitmask Jul 27 '12 at 15:19
  • @MoazELdeen: how do you plan to use this facility? Do you know who are the entities who shares the ownership of the wrapped object? Do you really know what "object ownership" means? What's the lifetime of those object, btw? Do you have a policy which states when to remove a strategy or when to keep it, should others need it? etc. – akappa Jul 27 '12 at 15:21
  • @bitmask: cppreference just describes the API, and no one is supposed to clone that into an answer. The point is that, most probably, he doesn't know nothing about memory management, which is something more abstract and general than just a shared_ptr. – akappa Jul 27 '12 at 15:22
  • @akappa can you just a referene for memory management issues like this? – Moaz ELdeen Jul 27 '12 at 15:24
  • @MoazELdeen: http://stackoverflow.com/questions/76796/memory-management-in-c – akappa Jul 27 '12 at 15:26
  • @akappa: You are right. But this cppreference page gives a quick introduction into what a shared_ptr is and how it works. If that isn't enough, there are enough resources both here on SO and on the internet in general. – bitmask Jul 27 '12 at 15:26
  • @bitmask: I agree, there is a lot of informations available. I just wanted to point out that he should read something about general, manual memory management concepts and techniques, and then pick up the right tools for the job (a shared_ptr, with high probability). – akappa Jul 27 '12 at 15:28