locked
Disabling Garbage Collector C++/XAML

    Question

  • Hi everybody,

    I'm currently starting with a c++ project for my study and I ren into a problem and I'm hoping you guys can help me out.

    I must make a program in c++ and I would love too make a windows 8 app. The only problem is that in this version of c++ is managed so the Garbage Collector automatically disposes unused objects. And school demands that I do that myself. Is there any way you can disable the garbage collector?

    Greetz,

    Soepvork

    Monday, November 5, 2012 11:02 PM

Answers

  • Hi Soepvork,

    There is no magic memory management here. Your app is leaking memory (arguably: how to you differentiate a leak from a cache?), but you don't call any code to detect it. If you put a call to _CrtDumpMemoryLeaks() in you will see a number of leaked blocks.

    I think the confusion here is because of the different application lifecycle model for Windows Store apps than from desktop apps. Windows Store apps do not exit in the same way as desktop apps: they suspend their processor usage and leave their memory whole. If the memory is needed for other apps then it is disposed of without the app getting a further chance to run. It is expected that apps will leave memory allocated when the app is killed.

    Because of this, the CRT's attempt to _CrtDumpMemoryLeaks when the app exits never runs.

    You can also use the CRT functions to do your own check pointing and dumping to see what memory has been allocated and not released between the two calls. You will need to then analyze the difference to determine if the memory usage is intentional or if it is a leak. Take a look at the CRT Debug Heap documentation for more information on how to do so.

    --Rob

     

    • Marked as answer by Jesse Jiang Friday, November 23, 2012 8:02 AM
    Monday, November 12, 2012 9:33 PM
    Owner

All replies

  • C++/Cx does not use a garbage collector. Ref classes are reference-counted though, so you don't explicitly manage their lifetime. If you create classic C++ classes rather than ref classes then you must use classic C++ memory management.

    --Rob

    • Proposed as answer by Chris Guzak Tuesday, November 6, 2012 5:57 AM
    • Marked as answer by Jesse Jiang Monday, November 12, 2012 2:32 AM
    • Unmarked as answer by Soepvork1 Monday, November 12, 2012 6:14 PM
    Tuesday, November 6, 2012 1:00 AM
    Owner
  • Thanks for your reply Rob,

    The last week we have tested a Windows Store app too see if it meets the schools requirements. But we only encountered a problem when we intentionally try too test for a possitive memory leak.

    When we created a instance of our own class (without ref) it didn't showed up as a memory leak. We have found that standard memory leak report is disabled but found a replacement. But it doesn't show any leaks. We don't know anymore if we we do something wrong or that the app secretly manages all objects anyway. Hopefully there is a solution because we would love too do a Windows 8 app but we have too prove that its unmanaged

    Here is the code we used:

    // MyModel Class
    class MyModel
    {
    public:
    	MyModel();
    	~MyModel();
    
    public:
    	std::string *pText;
    };
    
    // App.xaml.cpp
    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include "pch.h"
    #include <crtdbg.h>
    #include "MainPage.xaml.h"
    
    App::App()
    {
    	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
        _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
    
        int *pArray = (int*)malloc(sizeof(int) * 24); // Memory not freed
    
           InitializeComponent();
    }
    
    // MainpPage.xaml.cpp
    #include "MainPage.xaml.h"
    #include "MyModel.h"
    #include <string>
    
    MainPage::MainPage()
    {
    	InitializeComponent();
    
    	myModel = new MyModel(); // Intentional memoryleak, won't be deleted anywhere. But still no memory leak found
    }
    

    Monday, November 12, 2012 6:26 PM
  • Hi Soepvork,

    There is no magic memory management here. Your app is leaking memory (arguably: how to you differentiate a leak from a cache?), but you don't call any code to detect it. If you put a call to _CrtDumpMemoryLeaks() in you will see a number of leaked blocks.

    I think the confusion here is because of the different application lifecycle model for Windows Store apps than from desktop apps. Windows Store apps do not exit in the same way as desktop apps: they suspend their processor usage and leave their memory whole. If the memory is needed for other apps then it is disposed of without the app getting a further chance to run. It is expected that apps will leave memory allocated when the app is killed.

    Because of this, the CRT's attempt to _CrtDumpMemoryLeaks when the app exits never runs.

    You can also use the CRT functions to do your own check pointing and dumping to see what memory has been allocated and not released between the two calls. You will need to then analyze the difference to determine if the memory usage is intentional or if it is a leak. Take a look at the CRT Debug Heap documentation for more information on how to do so.

    --Rob

     

    • Marked as answer by Jesse Jiang Friday, November 23, 2012 8:02 AM
    Monday, November 12, 2012 9:33 PM
    Owner
  • Hi Rob,

    Thanks for your quick responds.

    Yea I've come across that function yes. But i figured it wouldn't be much good because it just prints all the memory currently being allocated. (If I got it right). But you are saying that I should calculate the difference myself too see if I got any leaks. I'll throw it in the group and see if we can make any progress with this. Thanks for the clarification.

    Greetz

    Monday, November 12, 2012 11:40 PM