Solved

Array allocations from within templates

Posted on 2000-03-21
6
185 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries DB2 Query 2 97
sum67 challenge 35 110
post4 challenge 28 105
Use of condition with 'serial' in ansible 2 71
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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