Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array allocations from within templates

Posted on 2000-03-21
6
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 800 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

722 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