Solved

Array allocations from within templates

Posted on 2000-03-21
6
182 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 65
Pressing cancel button with C# UI Automation 3 51
format the code in java 6 86
mapAB Challlenge 35 127
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

896 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

14 Experts available now in Live!

Get 1:1 Help Now