Solved

Array allocations from within templates

Posted on 2000-03-21
6
181 Views
Last Modified: 2013-11-20
I have a DLL which contains a class, and one of its members is a template class instantiation. The template class allocates and subsequently deletes an array of an arbitrary type.

The problem occurs when the DLL is used from a MFC program in debug mode. The DLL is a regular Win32 DLL - not MFC.

When the array is destructed I get an assertion failure in dbgheap.c at line 1076. If the assertion is ignored everything works fine.

Hopefully the following code example will clarify this:

template<class T>
class CMyTempl<T> {
private:
    T *m_pT;
public:
    CMyTempl(int x) {
        m_pT = new T[x];
    }
    ~CMyTempl() {
        // This is where it all goes TU
        delete [] m_pT;
    }
};

struct SMyStruct {
    int a;
    short b;
    string c;
};

class CMyClass {
public:
    CMyTempl<SMyStruct> m_T;

    CMyClass() : m_T(123) {};
};

I figure the problem must be some clash between CRT memory checking and MFC memory checking. Hope you guys can help...
0
Comment
Question by:andymurd
6 Comments
 

Expert Comment

by:Subramanya
ID: 2641635

it is delete problem.
Remove the [] and make it like below.
~CMyTempl() {
        // This is where it all goes TU
        delete  m_pT;
    }
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2642592
WHAT!!

If you allocate with new T[..]; then you MUST deallocate with delete []p.

Thems the (C++) rules .. suggesting that you remove the [] from the delete is WRONG.

But what IS worth checking is that in your REAL code, you do actuallyallocate an array with new T[..] and didn't mistakenly do a new T(...).


0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2642599
It could well be that something ELSE is stuffing up and corrupting the memory.  And you don't find out until the (perfectly valid) delete happens.

It may even be something happening to another allocated lump of memory altogether (not even directly in the array in question).

Put some call to check the heap throughout your code and see .. or (if you have it) use something like BoundsChecker to find out who is stuffing up.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:andymurd
ID: 2643794
Subramanya, as RONSLOW says I must deallocate with the delete[] operator to ensure that the destructors are called for object in the array.

I am definitely allocating with the new[] operator. Unfortunately I don't have Boundschecker I'll see if CrtCheckMemory will make any sense.
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 200 total points
ID: 2643878
Hi andymurd?

Do both (the MFC app and the DLL) use the same C run-time library? Check this in 'Project Settings' tab 'C++' category 'code generation' combo box 'Use run-time library'.

ZOPPO
0
 
LVL 1

Author Comment

by:andymurd
ID: 2644048
Excellent answer Zoppo - have 200 points. Any ideas why the CRT DLL and static libraries are incompatible?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now