simple scope question

This is a discussion on simple scope question within the C++ Programming forums, part of the General Programming Boards category; The memory COULD be unsecure. However, it's the object manager's duty to make sure that it isn't. COM objects keep ...

  1. #16
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    The memory COULD be unsecure. However, it's the object manager's duty to make sure that it isn't. COM objects keep track of how many pointers/references you have to them, and only free the memory until they are all gone (if you Release properly).

  2. #17
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hey, would you mind offering your clarification here for me.
    Not being familiar with object factories, I can only offer speculation, and it'll be based on the definition of object factory here (even though it's from Java):
    http://java.sun.com/products/jndi/tu...jects/factory/

    So my guess is, an object factory works something like this:
    Code:
    SomeObject createObject(int arg1, int arg2, double arg3)
    {
       return SomeObject(arg1, arg2, arg3, someDataOnlyThisFunctionCanAccess);
    }
    
    ...
    
    SomeObject s = createObject(1, 2, 3.4);
    So what happens is, a SomeObject object is constructed using the data provided, and this object is returned - i.e. a copy of it is made, and then s is constructed by some method that involves the returned copy - and then the return statement ends, and this temporarily constructed SomeObject object goes out of scope and is destroyed, and the copy that s was constructed from also goes out of scope and is destroyed. But s has already been constructed/copied in some sequence from the originally created object, and therefore since it is a copy and not a pointer to the original, it is not destroyed.

    Alternately, the object factory might return a pointer to a dynamically created object (i.e. created using new), and therefore the object never goes out of scope and must be deleteed manually.

    >>as long as there was a viable pointer to a memory location than the object persisted
    You can emulate this using smart pointers that implement reference-counting (take a look here for an example of how it can be done manually), but it is by no means automatically done for you. As far as I know, COM uses reference counting, and if you forget to free a resource then the reference count never reaches 0 and the resource is never destroyed as it should be.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #18
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ahh I see. Thankyou Hunter2 and skorman00; that was very enlightening.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Shatki,

    Great example. Thanks.

    skorman00,

    Thanks for your responses, too.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  2. A simple question about scope
    By dwylie in forum C Programming
    Replies: 3
    Last Post: 12-10-2004, 01:16 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM
  5. Scope question
    By mikebrewsj in forum C++ Programming
    Replies: 1
    Last Post: 01-17-2002, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21